java三线程交替打印123……n
2015-03-11 17:24
267 查看
使用多线程交替打印1--n,a进程打印1,4,7,……(3n+1),b进程打印2,7,10,……(3n+2),c进程打印3,6,9,……(3n)
涉及到多线程的同步,阻塞,wait,notify
代码如下
Num.java
public class Num {
private int num = 0;
public Num(int num) {
this.num = num;
}
public synchronized void printOne() {
try {
while (num%3!=1) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void printTwo() {
try {
while (num%3!=2) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void printThr() {
try {
while (num%3!=0) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}三个线程类
public class PrintOne implements Runnable {
private Num num;
public PrintOne(Num num) {
this.num = num;
}
@Override
public void run() {
while (true) {
num.printOne();
}
}
}
测试类
public class Test {
public static void main(String[] args) {
Num num = new Num(0);
Thread pOne = new Thread(new PrintOne(num));
Thread pTwo = new Thread(new PrintTwo(num));
Thread pThr = new Thread(new PrintThr(num));
pOne.setName("3n+1");
pTwo.setName("3n+2");
pThr.setName("3n ");
pOne.start();
pTwo.start();
pThr.start();
}
}
效果
涉及到多线程的同步,阻塞,wait,notify
代码如下
Num.java
public class Num {
private int num = 0;
public Num(int num) {
this.num = num;
}
public synchronized void printOne() {
try {
while (num%3!=1) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void printTwo() {
try {
while (num%3!=2) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void printThr() {
try {
while (num%3!=0) {
this.wait();
}
System.out.println(Thread.currentThread().getName() + "------->"
+ (num++));
Thread.sleep(1000);
this.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}三个线程类
public class PrintOne implements Runnable {
private Num num;
public PrintOne(Num num) {
this.num = num;
}
@Override
public void run() {
while (true) {
num.printOne();
}
}
}
public class PrintTwo implements Runnable { private Num num; public PrintTwo(Num num) { this.num = num; } @Override public void run() { while (true) { num.printTwo(); } } }
public class PrintThr implements Runnable { private Num num; public PrintThr(Num num) { this.num = num; } @Override public void run() { while (true) { num.printThr(); } } }
测试类
public class Test {
public static void main(String[] args) {
Num num = new Num(0);
Thread pOne = new Thread(new PrintOne(num));
Thread pTwo = new Thread(new PrintTwo(num));
Thread pThr = new Thread(new PrintThr(num));
pOne.setName("3n+1");
pTwo.setName("3n+2");
pThr.setName("3n ");
pOne.start();
pTwo.start();
pThr.start();
}
}
效果
3n ------->0 3n+1------->1 3n+2------->2 3n ------->3 3n+1------->4 3n+2------->5 …………
相关文章推荐
- java三线程交替打印123……n
- 使用Java线程并发库实现两个线程交替打印的线程题
- Java两个线程交替打印(实现Runnable接口)
- 两个线程交替打印字母 java
- 三个Java线程(使用线程池)交替打印1到75个数的两种解法
- 使用Java线程并发库实现两个线程交替打印的线程题
- Java实现两个线程交替打印问题
- Java 多线程 子线程 交替打印 奇偶数
- java 线程交替打印1-100值
- 三个Java线程(使用线程池)交替打印1到75个数的两种解法
- 黑马程序员——JAVA基础---线程之间的通信实现交替输出打印
- java 之 三个线程交替执行任务
- 一道Java线程面试题。3个线程顺序打印10次ABC.
- 迅雷笔试题 (JAVA多线程)启动三个线程,分别打印A B C,现在写一个程序 循环打印ABCABCABC
- java实现让n个线程严格交替执行
- java让2个线程交替执行
- java 三个线程分别打印A B C 50次
- java 实现3个线程 依次打印ABC,并且循环10次
- java实现【有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABC.】
- Java 3个线程依次打印ABC,并且循环10次