Title: Threads In Java CSS 2267 Under Construction
1Threads In Java - CSS 2267 Under Construction
2Threads and Multi-threading
- Concept of simultaneous independent
programs/processes running in same computer - Each in its own execution state
- Running in parallel
- Can have the same form of multiple independent
parallel activity inside on program/process - Inside one process, multiple independent threads
of execution - Arises naturally when one process needs to
support simultaneous independent activities - As in GUIs or doing heavy I/O
- You can write Java programs with such multiple
independent threads of activity - Unconsciously, or
- With intended and controlled effects in a run()
method
3Running a Thread
- Extend the Thread class and then use start( )
- As in first example at http//clem.mscd.edu/evan
sell/LECTURES/CodeExamplesThreads.htmlCreateThrea
ds - Class Show1 class Show1 extends Thread
- Has a run( ) method public void run()
- How outsider runs it
- Show1 s1 new Show1("First Real") s1.start()
- Implement the Interface Runnable
- As in second example at http//clem.mscd.edu/eva
nsell/LECTURES/CodeExamplesThreads.htmlCreateThre
ads - Class Show2 class Show2 implements Runnable
- Has a run( ) method public void run()
- How outsider runs it
- Show2 i2 new Show2()
- Thread s2 new Thread(i2, "First Interface")
- s2.start()
- Regardless the run() method does the work of the
thread
4States of a Thread in Java VERY Simplified
To be born
start( )
Ready
Various forms of waiting
yield( ) or pre-empted
scheduled
Run (Running)
run( ) falls out
DEAD
5The Class Thread
- See http//java.sun.com/j2se/1.5.0/docs/api/java/
lang/Thread.html - Constructors Many
- Usually pass a name for Thread
- Plus, possibly a Runnable object
- Important static methods
- Get current running thread Thread
currentThread() - Make a thread sleep for a while void sleep(long
millis) - Give up CPU to let others run void yield( )
- Important non-static methods
- Get and set priority (getPriority( ),
setPriority( )) - Get and set name (getName( ), setName( ))
- Get started start( )
- Run and wait on join(long millis)
6States of a Thread in Java SimplifiedLetting
Go!
To be born
start( )
Ready
Time expired, or interrupt
Got I/O
Sleep (Sleeping)
Other forms of waiting
Blocked (I/O, etc.)
yield( ) or pre-empted
scheduled
Wait for I/O
sleep( )
Run (Running)
run( ) falls out
DEAD
7Object Locks and Synchronization
- Standard paradigm for a thread to call methods
in other objects - Famous producer/consumer model
- What if two different threads want to run in the
same method of the same object? - How to prevent then from messing each other up?
- Each object has a lock on it
- Objects class can designate code as synchronized
- Only one thread can get into a set of
synchronized code at a time - See example at http//clem.mscd.edu/evansell/LEC
TURES/CodeExamplesThreads.htmlSynchThreads - Mechanisms to control and discipline access are
not in the thread code, but rather in the code of
the object being accessed!
8Methods and Mechanisms in Object Class Useful to
Threads
- Mechanisms synchronizing a block of code
- Two forms of synchronized
- Rules of synchronized
- Method notify() Helping to unblock others
- Applies to those waiting for the lock on object
you are in - How do you let go?
- Waiting to access something
- Method wait()
- see if you get notified
- see if what you are waiting for happened
- Repetitive wait-ing
- The complementary and dual nature of notify and
wait
9Simplified Producer - Consumer
- See http//clem.mscd.edu/evansell/LECTURES/CodeEx
amplesThreads.htmlSynchThreads
SThing
put() wait() for empty
thingVal
get() wait() for full
All outside threads that want to access SThing
have to wait for LOCK!
LOCK
10States of a Thread in Java - Ultimate
To be born
start( )
Ready
Time expired, or interrupt
Obtained lock
Got I/O
notified
Sleep (Sleeping)
Wait (Waiting)
Seeking Lock (on object)
Blocked (I/O, etc.)
yield( ) or pre-empted
scheduled
Entering synchronized code in object
Wait for I/O
wait( )
sleep( )
Run (Running)
run( ) falls out
DEAD
11To be born
start( )
Ready
Time expired, or interrupt
Obtained lock
Got I/O
notified
Sleep (Sleeping)
Wait (Waiting)
Seeking Lock (on object)
Blocked (I/O, etc.)
yield( ) or pre-empted
scheduled
Entering synchronized code in object
Wait for I/O
wait( )
sleep( )
Run (Running)
run( ) falls out
DEAD