13_Review Lecture

Set9223372036854775807 xset0 assert xi 0 assert

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: d no4fy –  Deadlocks Example: Using the Runnable Interface to Create and Launch Threads •  Objective: Create and run three threads: –  The first thread prints the letter a 100 times. –  The second thread prints the letter b 100 times. –  The third thread prints the integers 1 through 100. TaskThreadDemo 27 The Static sleep(milliseconds) Method! The sleep(long mills) method puts the thread to sleep for the specified time in milliseconds. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows: public void run() { for (int i = 1; i <= lastNum; i++) { System.out.print(" " + i); try { if (i >= 50) Thread.sleep(1); } catch (InterruptedException ex) { } } } Every time a number (>= 50) is printed, the print100 thread is put to sleep for 1 millisecond. 28 The join() Method! You can use the join() method to force one thread to wait for another thread to finish. For example, suppose you modify the code in Lines 53-57 in TaskThreadDemo.java as follows: The numbers auer 50 are printed auer thread printC is finished. 29 Data Race (Low Level) class Ref { long t; void set (long i) { t = i; } } Ref x = new Ref(0)...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online