Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
111 lines (78 loc) · 2.23 KB

File metadata and controls

111 lines (78 loc) · 2.23 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import java.util.Random;
/**
* Created by mac on 2/13/17.
*/
public class Main {
public static void main(String[] args) {
Message message = new Message();
(new Thread(new Writer(message))).start();
(new Thread(new Reader(message))).start();
}
}
class Message {
private String message;
private boolean empty = true;
public synchronized String read(){
while(empty){
try{
wait();
} catch(InterruptedException e){
}
}
empty = true;
notifyAll();
return message;
}
public synchronized void write(String message){
while(!empty){
try{
wait();
}catch(InterruptedException e){
}
}
empty = false;
this.message = message;
notifyAll();
}
}
class Writer implements Runnable{
private Message message;
public Writer(Message message){
this.message = message;
}
public void run()
{
String messages[] = {
"Humpty Dumpty sat on a wall",
"Humty Dumpty had a great fall",
"All the king's horses and all the men",
"Couldn't put Humpty together again"
};
//create instance of a random class so we could have a random delay
Random random = new Random();
for(int i =0; i<messages.length; i++){
message.write(messages[i]);
try{
Thread.sleep(random.nextInt(2000));
} catch (InterruptedException e){
}
}
message.write("Finished");
}}
class Reader implements Runnable{
private Message message;
public Reader(Message message){
this.message = message;
}
public void run(){
Random random = new Random();
for(String latestMessage = message.read(); !latestMessage.equals("Finished");
latestMessage = message.read()) {
System.out.println(latestMessage);
try{
Thread.sleep(random.nextInt(2000));
} catch(InterruptedException e){
}
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.