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
Embed
1Quiz Modify HelloWorld2.java; –Remove (or comment out) the following 4 lines: Thread thread1 = new Thread(runnable1); Thread thread2 = new Thread(runnable2);
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
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).
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, …
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.
21
• public class Thread { static void dumpStack() public StackTraceElement[] getStackTrace() public StackTraceElement[] getAllStackTrace()
– 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)