/**
* 利用线程通信原理
* 打印12A34B56C78D。。。。5152Z
*
* @author think
*/
public class Print {
public boolean flag = true;
public synchronized void printNum(){
for(int i = 1;i<=26;i++){
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print((2*i-1)+""+(2*i));
flag = false;
notifyAll();
}
}
public synchronized void printChar(){
for(char i='A';i<='Z';i++){//for循环不能让其停掉
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//如果要连续打印循环中的i的话,千万注意不能加else!!!加的话,flag为true时,程序wait,到被notify()之后,此处循环中else的代码走不到
//不加else的话,被notify()之后跳过if()还会向下走,打印此处循环中的i
// else{//
System.out.print(i);
flag = true;
notifyAll();
// }
}
}
public static void main(String[] args) {
Print p = new Print();
Thread t1 = new Thread(){
public void run(){
p.printNum();
}
};
Thread t2 = new Thread(){
public void run(){
p.printChar();
}
};
t1.start();
t2.start();
}
}
方法1:
package threadexxcommunication;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class Print {
public boolean flag = false;
public synchronized void print1(){
for(int i=0;i<=25;i++){
while(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print(2*i+1 +""+ (2*i+2));
flag = true;
notifyAll();
}
}
public synchronized void print2(){
for(char c='A';c<='Z';c++){
while(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print(c);
flag = false;
notifyAll();
}
}
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
Print p = new Print();
Thread target1 = new Thread() {
@Override
public void run() {
p.print1();
}
};
Thread target2 = new Thread() {
@Override
public void run() {
p.print2();
}
};
pool.submit(target1);
pool.submit(target2);
pool.shutdown();
}
}
方法2
package threadexxcommunication;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class PrintNew {
public boolean flag = false;
public int count =0;
public synchronized void printNum(String outputString) throws InterruptedException{
while(flag){
wait();
}
System.out.print(outputString);
count ++;
if((count+1) % 3 ==0){
flag=true;
notifyAll();
}
}
public synchronized void printChar(String outputString) throws InterruptedException{
while(!flag){
wait();
}
System.out.print(outputString);
count++;
flag =false;
notifyAll();
}
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
PrintNew p = new PrintNew();
Runnable task1 = new Runnable() {
@Override
public void run() {
for(int i=1;i<=52;i++){
try {
p.printNum(i+"");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
Runnable task2 = new Runnable() {
@Override
public void run() {
for(char c ='A';c<='Z';c++){
try {
p.printChar(c+"");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
};
pool.submit(task1);
pool.submit(task2);
pool.shutdown();
}
}