标题:测试线程通信,输出十次ABC
测试线程通信,输出十次ABC
注意要点:
- 打印十次,不是for循环十次,因为for循环十次时,有的线程进入后,就wait,然后没有执行到if中要执行的那部分,就出来了使得会少于十次,故需要再Account函数中加入判断,或者返回值,来结束while(true)
注意:
可能看到不连贯的ABC,因为系统的调度,此线程可能在输出“哈哈哈”前,被抢占了,
int in=account.printC();
//System.out.println("哈哈哈");
//可能看到不连贯的ABC,因为系统的调度,此线程可能在输出“哈哈哈”前,被抢占了,
/**
* 测试线程通信,输出ABC
* 打印十次,不是for循环十次,因为for循环十次时,有的线程进入后,就wait,然后没有执行到if中要执行的那部分,就出来了
* 使得会少于十次,故需要再Account函数中加入判断,或者返回值,来结束while(true)
* @author dell
*
*/
public class TestDemoThreadCom {
public static void main(String[] args) {
Account02 account=new Account02();
new PrintAThread(account).start();
new PrintBThread(account).start();
new PrintCThread(account).start();
}
}
class Account02{
private char cha='A';
private char chb='B';
private char chc='C';
private int count=1;
private int countA=1;
private int countB=1;
private int countC=1;
public synchronized int printA() {
if(1==count) {
System.out.print(cha);
count++;
countA++;
this.notifyAll();
}else {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return countA;
}
public synchronized int printB() {
if(2==count) {
System.out.print(chb);
count++;
countB++;
this.notifyAll();
}else {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return countB;
}
public synchronized int printC() {
if(3==count) {
System.out.print(chc);
count=1;
countC++;
this.notifyAll();
}else {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return countC;
}
}
class PrintAThread extends Thread{
private Account02 account;
public PrintAThread(Account02 account) {
this.account=account;
}
public void run() {
while(true) {
int in=account.printA();
if(10==in) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class PrintBThread extends Thread{
private Account02 account;
public PrintBThread(Account02 account) {
this.account=account;
}
public void run() {
while(true) {
int in=account.printB();
if(10==in) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class PrintCThread extends Thread{
private Account02 account;
public PrintCThread(Account02 account) {
this.account=account;
}
public void run() {
while(true) {
int in=account.printC();
// System.out.println("哈哈哈");//指令重排?
if(10==in) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
类似输出:
12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q3536R3738S3940T4142U4344V4546W4748X4950Y5152Z
/**
* 输出:12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q3536R3738S3940T4142U4344V4546W4748X4950Y5152Z
* 疯狂Java上的题目
* @author dell
*
*/
public class TestDemoThreadCom779 {
public static void main(String[] args) {
Account account=new Account();
new PrintIntThread(account).start();
new PrintCharThread(account).start();
}
}
class Account{
private int in=1;
private char ch='A';
private boolean flag=true;
public synchronized int printInt() {
if(flag) {
System.out.print((in++)+""+(in++));
flag=false;
this.notify();
}else {
try {
this.wait();//导致当前线程等待,此处main中开启了两个线程,
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return in-1;
}
public synchronized char printChar() {
if(!flag) {
System.out.print(ch++);
flag=true;
this.notify();
}else {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return (char)(ch-1);
}
}
class PrintIntThread extends Thread{
private Account account;
public PrintIntThread(Account account) {
super();
this.account = account;
}
public void run() {
while(true) {
int in=account.printInt();
if(52==in) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}
class PrintCharThread extends Thread{
private Account account;
public PrintCharThread(Account account) {
super();
this.account = account;
}
public void run() {
while(true) {
char ch=account.printChar();
// System.out.println();
if('Z'==ch) {
break;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
}