forked from haobinaa/DataStructure-DesignPattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrintOddEvenNumber.java
58 lines (49 loc) · 1.33 KB
/
PrintOddEvenNumber.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
55
56
57
58
/**
* BrandBigData.com Inc. Copyright (c) 2018 All Rights Reserved.
*/
package com.haobin.codeBlock;
/**
* wait/notify机制
* 2个多线程交替打印1-100内数字,其中t1线程打印奇数,t2程打印偶数\
* 关键点: print方法的锁两个线程交替获取
*
* @author HaoBin
* @version $Id: PrintOddEvenNumber.java, v0.1 2019/1/7 22:17 HaoBin
*/
public class PrintOddEvenNumber {
//定义打印的方法
public synchronized void print(String str){
notify();
System.out.println(str);
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//定义打印奇数的线程类
class A implements Runnable{
@Override
public void run() {
for(int i=1;i<100;i+=2){
print("A"+i);
}
}
}
//定义打印偶数的线程类
class B implements Runnable{
@Override
public void run() {
for(int i=2;i<=100;i+=2){
print("B"+i);
}
}
}
public static void main(String[] args) {
PrintOddEvenNumber printOddEvenNumber = new PrintOddEvenNumber();
A a = printOddEvenNumber.new A();
B b = printOddEvenNumber.new B();
new Thread(a).start();
new Thread(b).start();
}
}