两个线程交叉打印(最简单的)
2015-07-04 16:31
387 查看
A:线程:
package Thread.a.b.turn;
public class ThreadA implements Runnable {
private ThreadBusiness treadMian =null;
public ThreadA(ThreadBusiness threadMain ){
this.treadMian=threadMain;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
treadMian.printA();
}
}
}
B线程:
package Thread.a.b.turn;
public class ThreadB implements Runnable {
private ThreadBusiness treadMian =null;
public ThreadB(ThreadBusiness threadMain ){
this.treadMian=threadMain;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
treadMian.printB();
}
}
}
业务逻辑:
package Thread.a.b.turn;
public class ThreadBusiness {
public boolean flag=true;
public synchronized void printA(){
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag=false;
System.out.println("A");
notify();
}
public synchronized void printB(){
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag=true;
System.out.println("B");
notify();
}
}
测试:
package Thread.a.b.turn;
public class TestThreadAB {
public static void main(String args[]){
//先让A线程打印,后让B线程打印
ThreadBusiness threadMain = new ThreadBusiness();
Thread a = new Thread(new ThreadA(threadMain),"A");
Thread b = new Thread(new ThreadB(threadMain),"B");
a.start();
b.start();
}
}
希望大家赐教。
package Thread.a.b.turn;
public class ThreadA implements Runnable {
private ThreadBusiness treadMian =null;
public ThreadA(ThreadBusiness threadMain ){
this.treadMian=threadMain;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
treadMian.printA();
}
}
}
B线程:
package Thread.a.b.turn;
public class ThreadB implements Runnable {
private ThreadBusiness treadMian =null;
public ThreadB(ThreadBusiness threadMain ){
this.treadMian=threadMain;
}
public void run() {
// TODO Auto-generated method stub
for(int i=0;i<10;i++){
treadMian.printB();
}
}
}
业务逻辑:
package Thread.a.b.turn;
public class ThreadBusiness {
public boolean flag=true;
public synchronized void printA(){
if(!flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag=false;
System.out.println("A");
notify();
}
public synchronized void printB(){
if(flag){
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag=true;
System.out.println("B");
notify();
}
}
测试:
package Thread.a.b.turn;
public class TestThreadAB {
public static void main(String args[]){
//先让A线程打印,后让B线程打印
ThreadBusiness threadMain = new ThreadBusiness();
Thread a = new Thread(new ThreadA(threadMain),"A");
Thread b = new Thread(new ThreadB(threadMain),"B");
a.start();
b.start();
}
}
希望大家赐教。
相关文章推荐
- 真实故事:网站遭遇DOS攻击
- Windows上的文件合并命令
- hive sql 查询 Child Error 错误追究
- 1、uc/os:进程、线程、(void*)0 的简单解释
- PAT 1011 World Cup Betting
- JAVA中的继承和覆盖
- 谈一下最近期的学习计划
- opencv图像基本操作算法
- 黑马程序员——程序流程控制
- SNAT , DNAT区别
- android imageButton 透明图片
- 网页制作必须设置的一些属性
- Android开发(三十)——ScrollView中ListView的高度自动适应
- vim 命令
- PAT 1008 Elevator
- UVA - 116 Unidirectional TSP
- “用户体验不是把每个环节做到极致,而是在关键环节打动用户“这句话对吗?
- linux下的shell命令的编写,以及java如何调用linux的shell命令(java如何获取linux上的网卡的ip信息)
- Shell基础
- java中解析日期SimpleDateFormat失败后如何处理呢