Java多線程實(shí)現(xiàn)同時(shí)輸出
一道經(jīng)典的面試題目:兩個(gè)線程,分別打印AB,其中線程A打印A,線程B打印B,各打印10次,使之出現(xiàn)ABABABABA.. 的效果
package com.shangshe.path;
public class ThreadAB {
/**
* @param args
*/
public static void main(String[] args) {
final Print business = new Print();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<10;i++) {
business.print_A();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<10;i++) {
business.print_B();
}
}
}).start();
}
}
class Print {
private boolean flag = true;
public synchronized void print_A () {
while(!flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("A");
flag = false;
this.notify();
}
public synchronized void print_B () {
while(flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("B");
flag = true;
this.notify();
}
}
由上面的例子我們可以設(shè)計(jì)出3個(gè)線程乃至于n個(gè)線程的程序,下面給出的例子是3個(gè)線程,分別打印A,B,C 10次,使之出現(xiàn)ABCABC.. 的效果
public class ThreadABC {
/**
* @param args
*/
public static void main(String[] args) {
final Print business = new Print();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++) {
business.print_A();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++) {
business.print_B();
}
}
}).start();
new Thread(new Runnable() {
public void run() {
for(int i=0;i<100;i++) {
business.print_C();
}
}
}).start();
}
}
class Print {
private boolean should_a = true;
private boolean should_b = false;
private boolean should_c = false;
public synchronized void print_A () {
while(should_b || should_c) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("A");
should_a = false;
should_b = true;
should_c = false;
this.notifyAll();
}
public synchronized void print_B () {
while(should_a || should_c) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("B");
should_a = false;
should_b = false;
should_c = true;
this.notifyAll();
}
public synchronized void print_C () {
while(should_a || should_b) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.print("C");
should_a = true;
should_b = false;
should_c = false;
this.notifyAll();
}
}
再一次證明了軟件工程的重要性了;在多線程程序中,應(yīng)該說(shuō)在程序中,我們應(yīng)該把那些業(yè)務(wù)邏輯代碼放到同一個(gè)類中,使之高內(nèi)聚,低耦合
- Java Web項(xiàng)目中使用Socket通信多線程、長(zhǎng)連接的方法
- java多線程編程之Synchronized關(guān)鍵字詳解
- 詳解Java多線程編程中的線程同步方法
- JAVA多線程之中斷機(jī)制stop()、interrupted()、isInterrupted()
- java多線程抓取鈴聲多多官網(wǎng)的鈴聲數(shù)據(jù)
- Java多線程編程中線程鎖與讀寫鎖的使用示例
- java多線程之線程安全的單例模式
- 詳解Java實(shí)現(xiàn)多線程的三種方式
- Java 實(shí)現(xiàn)多線程的幾種方式匯總
- Java單利模式與多線程總結(jié)歸納
- 深入理解JAVA多線程之線程間的通信方式
相關(guān)文章
Java SpringBoot開(kāi)發(fā)小技巧詳解
這篇文章主要介紹了淺談SpringBoot項(xiàng)目如何讓前端開(kāi)發(fā)提高效率(小技巧),主要介紹了Swagger和Nginx提高效率的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
SpringBoot中Formatter和Converter用法和區(qū)別小結(jié)
本文主要介紹了SpringBoot中Formatter和Converter用法和區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
解決spring-boot 打成jar包后 啟動(dòng)時(shí)指定參數(shù)無(wú)效的問(wèn)題
這篇文章主要介紹了解決spring-boot 打成jar包后 啟動(dòng)時(shí)指定參數(shù)無(wú)效的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
使用Mybatis實(shí)現(xiàn)分頁(yè)效果示例
大家好,本篇文章主要講的是使用Mybatis實(shí)現(xiàn)分頁(yè)效果示例,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
Java中JDBC事務(wù)與JTA分布式事務(wù)總結(jié)與區(qū)別
Java事務(wù)的類型有三種:JDBC事務(wù)、JTA(Java Transaction API)事務(wù)、容器事務(wù),本文詳細(xì)介紹了JDBC事務(wù)與JTA分布式事務(wù),有需要的可以了解一下。2016-11-11

