スレッド反発
3308 ワード
ここでは、教材を参照して書いたリードスレッドとライトスレッドを学習します.2つのスレッドには反発関係があります.
実行結果は次のとおりです.
write:data 0, i = 1, count = 20read:data 0, i = 1, count = 20write:data 1, i = 2, count = 20read:data 1, i = 2, count = 20write:data 2, i = 3, count = 20read:data 2, i = 3, count = 20write:data 3, i = 4, count = 20read:data 3, i = 4, count = 20write:data 4, i = 5, count = 20read:data 4, i = 5, count = 20write:data 5, i = 6, count = 20read:data 5, i = 6, count = 20write:data 6, i = 7, count = 20read:data 6, i = 7, count = 20write:data 7, i = 8, count = 20read:data 7, i = 8, count = 20write:data 8, i = 9, count = 20read:data 8, i = 9, count = 20write:data 9, i = 10, count = 20read:data 9, i = 10, count = 20write:data 10, i = 11, count = 20read:data 10, i = 11, count = 20write:data 11, i = 12, count = 20read:data 11, i = 12, count = 20write:data 12, i = 13, count = 20read:data 12, i = 13, count = 20write:data 13, i = 14, count = 20read:data 13, i = 14, count = 20write:data 14, i = 15, count = 20read:data 14, i = 15, count = 20write:data 15, i = 16, count = 20read:data 15, i = 16, count = 20write:data 16, i = 17, count = 20read:data 16, i = 17, count = 20write:data 17, i = 18, count = 20read:data 17, i = 18, count = 20write:data 18, i = 19, count = 20read:data 18, i = 19, count = 20write:data 19, i = 20, count = 20read:data 19, i = 20, count = 20
import java.util.LinkedList;
public class WriteRead {
public static void main(String args[]) {
WriteThread writeThread = new WriteThread();
for (int i = 0; i < 20; i++) {
writeThread.addElement("data " + i);
}
ReadThread readThread = new ReadThread(writeThread);
writeThread.start();
readThread.start();
}
}
class WriteThread extends Thread {
boolean empty = true;
int i = 0;
int count = 0;
LinkedList<String> list = new LinkedList<String>();
public void addElement(String str) {
list.add(str);
count++;
}
public void run() {
while (true) {
write();
}
}
synchronized void write() {
while (!empty || i >= count) {
try {
wait();
} catch (Exception e) {
}
}
toSleep();
++i;
System.out.println("write:" + list.get(i - 1) + ", i = " + i
+ ", count = " + count);
empty = false;
notifyAll();
}
void toSleep() {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
synchronized void consume() {
while (empty) {
try {
wait();
} catch (Exception e) {
}
}
toSleep();
System.out.println("read:" + list.get(i - 1) + ", i = " + i
+ ", count = " + count);
empty = true;
notifyAll();
}
}
class ReadThread extends Thread {
WriteThread writeThread;
ReadThread(WriteThread writeThread) {
this.writeThread = writeThread;
}
public void run() {
while (true) {
writeThread.consume();
}
}
}
実行結果は次のとおりです.
write:data 0, i = 1, count = 20read:data 0, i = 1, count = 20write:data 1, i = 2, count = 20read:data 1, i = 2, count = 20write:data 2, i = 3, count = 20read:data 2, i = 3, count = 20write:data 3, i = 4, count = 20read:data 3, i = 4, count = 20write:data 4, i = 5, count = 20read:data 4, i = 5, count = 20write:data 5, i = 6, count = 20read:data 5, i = 6, count = 20write:data 6, i = 7, count = 20read:data 6, i = 7, count = 20write:data 7, i = 8, count = 20read:data 7, i = 8, count = 20write:data 8, i = 9, count = 20read:data 8, i = 9, count = 20write:data 9, i = 10, count = 20read:data 9, i = 10, count = 20write:data 10, i = 11, count = 20read:data 10, i = 11, count = 20write:data 11, i = 12, count = 20read:data 11, i = 12, count = 20write:data 12, i = 13, count = 20read:data 12, i = 13, count = 20write:data 13, i = 14, count = 20read:data 13, i = 14, count = 20write:data 14, i = 15, count = 20read:data 14, i = 15, count = 20write:data 15, i = 16, count = 20read:data 15, i = 16, count = 20write:data 16, i = 17, count = 20read:data 16, i = 17, count = 20write:data 17, i = 18, count = 20read:data 17, i = 18, count = 20write:data 18, i = 19, count = 20read:data 18, i = 19, count = 20write:data 19, i = 20, count = 20read:data 19, i = 20, count = 20