-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example17.java
62 lines (51 loc) · 1.05 KB
/
Example17.java
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
class Storage{
private int [] cells = new int [10];
private int inpos,outpos;
public void put (int num){
cells[inpos]=num;
System.out.println("在cells["+inpos+"]中放入数据---"+cells[inpos]);
inpos++;
if(inpos==cells.length)
inpos=0;
}
public void get (){
@SuppressWarnings("unused")
int data = cells[outpos];
System.out.println("在cells["+inpos+"]中取出数据---"+cells[inpos]);
outpos++;
if(outpos==cells.length)
outpos=0;
}
}
class Input implements Runnable {
private Storage st;
private int num;
Input (Storage st){
this.st=st;
}
public void run (){
while (true){
st.put(num++);
}
}
}
class Output implements Runnable {
private Storage st;
Output (Storage st ){
this.st =st ;
}
public void run (){
while (true ){
st.get();
}
}
}
public class Example17 {
public static void main (String [] args){
Storage st =new Storage ();
Input input =new Input (st);
Output output =new Output(st);
new Thread(input).start();
new Thread(output).start();
}
}