Link to home
Start Free TrialLog in
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on

Crash course in java threads

Hi,

Can someone provide me with a snippet or good link to show how to create a 'worker' thread in java. I don't know if that terminology is used, Im takin it from my C++ stuff. I would like to do the following:

1) Start the main application
2) Create a new thread - have it open a local file.
3) The new thread notifies the main thread that it's terminating
4) The main thread prints something like "loading....." while it's waiting for the new thread to finish.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sounds like the SwingWorker is what you need.
Have a look at SwingWorker in 1.6
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Yeah but I read swing was a huge download for the end user? I want my applet to be lightweight, isn't there any thread stuff built into the java core? I really have no idea what I'm talking about since I'm new to java, just thought there'd be something built in. Please feel free to correct me!
swing is part of the standard install (has been since java2)
There's nothing to download. Swing will already be in the JRE of the user
And SwingWorker is standard in 1.6. And is only a single class if you want to support earlier versions of java.

see the links I posted earlier for how to use it.
heres an example that shows it being used to do some long calculations and update the gui for the progress

http://www.java2s.com/Code/Java/Swing-JFC/SwingworkerExample.htm

And some background
http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html