220 likes | 286 Views
Quiz. Modify HelloWorld2.java; Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2); thread1.start(); thread2.start(); Do this: runnable1.run(); runnable2.run();
E N D
Quiz • Modify HelloWorld2.java; • Remove (or comment out) the following 4 lines: • Thread thread1 = new Thread(runnable1); • Thread thread2 = new Thread(runnable2); • thread1.start(); • thread2.start(); • Do this: • runnable1.run(); • runnable2.run(); • Describe the result of the modified program and explain why the result looks like that (why it is different from the result of the original Code 2).
Thread State Waiting notify()notifyAll() signal()signalAll()interruption TimedWaiting sleep()join()wait()await() notify()notifyAll()signal() signalAll() interruption sleep()join()wait()await() Exits run() or Explicit threadtermination Runnable Terminated new New start() I/O op completionor thread sync done I/O operation or wait for thread sync (lock) Blocked
New • A Thread object is created. start() has not called on the object yet. • Runnable • Java does not distinguish runnable and running. A running thread is still in the Runnable state. • Dead • A thread automatically dies when run() returns.
public class Thread { public enum State { NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED } public Thread.State getState() public boolean isAlive() • Alive • in the Runnable, Blocked, Waiting or Timed Waiting state. • isAlive() is usually used to poll a thread to see if it is terminated.
GreetingThreadRunner.java • Output: • NEW • NEW • false • false • RUNNABLE • RUNNABLE • true • true • Wed Mar 28 04:25:01 EDT 2007 Goodbye World • Wed Mar 28 04:25:01 EDT 2007 Hello World • Wed Mar 28 04:25:02 EDT 2007 Hello World • Wed Mar 28 04:25:02 EDT 2007 Goodbye World • Wed Mar 28 04:25:03 EDT 2007 Hello World • Wed Mar 28 04:25:03 EDT 2007 Goodbye World • …… • TERMINATED • TERMINATED • false • false
Thread Termination • Implicit termination • A thread triggers its own death when run() returns. • Once a thread starts executing run(), it continues execution until it returns. • Explicit termination • A thread is terminated by another thread. • when a certain condition is met. • e.g. web browser • Two ways • Setting a flag • Interrupting a thread
Thread Termination Flag • Stop a thread by setting a flag to signal that the thread should stop. • The thread periodically checks out the flag to determine if it should exit.
Main thread Thread t t.start() Prints #s Printing 10 Sleeps for 1 sec t.setDone() Prints “stopped bymain()” t.join() Waits for t.run()to return. Goes to theWaiting state. t.start() Printing 9, 8, 7, …
Output: • # • 10 • # • # • # • # • stopped by main()! • 9 • 8 • 7 • 6 • 5 • 4 • 3 • 2 • 1 • 0 • print done
Thread Interruption • Interrupt a thread to minimize this delay. • Signals a thread that the thread should exit. • Thread.interrupt()
InterruptableTask.java Main thread Thread t t.start() Printing 1 Sleeps for 1 sec Goes to the Waitingstate. t.interrupt() Interrupted! Goes to the Runnablestate. CatchesInterruptedException A try-catch clause is always necessary to catch InterruptedException, when using sleep().
InterruptableTask2.java Main thread Thread t t.start() Keep printing 1s Sleeps for 2 sec Goes to the Waitingstate. t.interrupt() Interrupted! Prints 4 Interrupt() does not stop a target thread.
Main thread Thread t t.start() Prints #s Prints 10 Sleeps for 1 sec Goes to the Waitingstate. t.interrupt() Interrupted! Goes to the Runnablestate. CatchesInterruptedException t.join() Waits for t.run()to return. Goes to theWaiting state. t.start() Printing 9, 8, 7, …
Output • # • 10 • # • # • # • # • interrupted by main()! • 9 • 8 • 7 • 6 • 5 • 4 • 3 • 2 • 1 • 0 • print done
SummationRunnable.java andSummationRunnableInterruptable.java t.start() t.start() Printing 10 Prints 10 Sleeps for 1 sec Goes to the Waitingstate. t.setDone() Sleeps for 1 sec t.interrupt() Interrupted! Goes to the Runnablestate. CatchesInterruptedException t.join() Prints “stopped bymain()” t.join() t.start() t.start() Printing 9, 8, 7, … Printing 9, 8, 7, …
Stack and Frames U Prog U data OS Res PCB TCB TCB TCB U K Stacks U K Stacks U K Stacks thread thread thread • User stack (Java stack) • One stack per thread • A set of blocks called frames. • Frame • associated with a method call. • Pushed when a method is called. • Poped when a method returns. Multi-threaded process • Contains • a table of local variables of the associated method • an operand stack containing the values of the partial results of the method • Program counter that points to the Java bytecode instruction currently being executed.
public class Thread { static void dumpStack() public StackTraceElement[] getStackTrace() public StackTraceElement[] getAllStackTrace()
InterruptableTask3.java • Output: • 1 • 3 • 4 • java.lang.Exception: Stack trace • at java.lang.Thread.dumpStack(Thread.java:1176) • at cs681.threads.InterruptableTask3.run(InterruptableTask3.java:17) • at java.lang.Thread.run(Thread.java:613)