-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample109.java
More file actions
35 lines (26 loc) · 764 Bytes
/
Example109.java
File metadata and controls
35 lines (26 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// Example 109 from page 83
//
class Printer extends Thread {
final static Object mutex = new Object();
@Override
public void run() {
for (;;) {
synchronized (mutex) {
System.out.print("-");
Util.pause(100,300);
System.out.print("/");
}
Util.pause(200);
} } }
class Example109 {
public static void main(String[] args)
{ new Printer().start(); new Printer().start(); }
}
// Pseudo-random numbers and sleeping threads
class Util {
public static void pause(int length)
{ try { Thread.sleep(length); } catch (InterruptedException x) {} }
public static void pause(int a, int b) { pause(random(a, b)); }
public static int random(int a, int b)
{ return (int)(a + (b-a+1) * Math.random()); }
}