Top Banner
1 Quiz 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).
22

1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

1

QuizQuiz

• 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).

Page 2: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

2

Thread StateThread State

Runnable

Blocked

newNew

start()

I/O op completionor thread sync done

I/O operation or wait for thread sync (lock)

Terminated

Exits run() orExplicit threadtermination

Waiting

sleep()join()wait()await()

notify()notifyAll()signal()signalAll()interruption

TimedWaitingsleep()

join()wait()await()

notify()notifyAll()signal()signalAll() interruption

Page 3: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

3

• 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.

Page 4: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

4

• 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.

Page 5: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

5

GreetingThreadRunner.javaGreetingThreadRunner.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

Page 6: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

6

Thread TerminationThread 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

Page 7: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

7

Thread Termination FlagThread 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.

Page 8: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

8

SummationRunnable.javaSummationRunnable.java

Page 9: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

9

Main thread Thread t

t.start()

Printing 10

Sleeps for 1 sect.setDone()

Prints “stopped bymain()”

Prints #s

t.join()Waits for t.run()to return. Goes to theWaiting state.

t.start()

Printing 9, 8, 7, …

Page 10: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

10

• Output:– #– 10– #– #– #– #– stopped by main()!– 9– 8– 7– 6– 5– 4– 3– 2– 1– 0– print done

Page 11: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

11

Thread Interruption Thread Interruption

• Interrupt a thread to minimize this delay. – Signals a thread that the thread should exit.– Thread.interrupt()

Page 12: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

12

InterruptableTask.javaInterruptableTask.java

Page 13: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

13

InterruptableTask.javaInterruptableTask.java

Main thread Thread t

t.start()

Printing 1

Sleeps for 1 secGoes to the Waitingstate.t.interrupt()

Interrupted!Goes to the Runnablestate.CatchesInterruptedException

A try-catch clause is always necessary tocatch InterruptedException, when using sleep().

Page 14: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

14

InterruptableTask2.javaInterruptableTask2.java

Page 15: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

15

InterruptableTask2.javaInterruptableTask2.java

Main thread Thread t

t.start()

Keep printing 1s

t.interrupt()

Interrupted!

Prints 4

Sleeps for 2 secGoes to the Waitingstate.

Interrupt() does not stop a target thread.

Page 16: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

16

SummationRunnableInterruptable.javaSummationRunnableInterruptable.java

Page 17: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

17

Main thread Thread t

t.start()

Prints 10

t.interrupt()

Prints #s

t.join()Waits for t.run()to return. Goes to theWaiting state.

t.start()

Printing 9, 8, 7, …

Sleeps for 1 secGoes to the Waitingstate.

Interrupted!Goes to the Runnablestate.CatchesInterruptedException

Page 18: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

18

• Output– #– 10– #– #– #– #– interrupted by main()!– 9– 8– 7– 6– 5– 4– 3– 2– 1– 0– print done

Page 19: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

19

SummationRunnable.java SummationRunnable.java andand

SummationRunnableInterruptable.javaSummationRunnableInterruptable.javat.start()

Prints 10

t.interrupt()

t.join()

t.start()

Printing 9, 8, 7, …

Sleeps for 1 secGoes to the Waitingstate.

Interrupted!Goes to the Runnablestate.CatchesInterruptedException

t.start()

Printing 10

Sleeps for 1 sect.setDone()

Prints “stopped bymain()”

t.join()

t.start()

Printing 9, 8, 7, …

Page 20: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

20

Stack and FramesStack and Frames• 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

TCB

U K Stacks

U Prog U data OS Res

thread

TCB

U K Stacks

thread

PCB

TCB

U K Stacks

thread

– 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.

Page 21: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

21

• public class Thread { static void dumpStack() public StackTraceElement[] getStackTrace() public StackTraceElement[] getAllStackTrace()

Page 22: 1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);

22

InterruptableTask3.javaInterruptableTask3.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)