java让2个线程交替执行,每个线程执行1秒
2017-10-17 11:07
411 查看
java让2个线程交替执行,每个线程执行1秒:
package tf56.SpringBoot.Annotation;
/**
* Created by Administrator on 2017/10/17.
*/
public class Test2 {
private static int state = 1;
public static void main(String[] args) {
final Test2 t=new Test2();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (t) {
if (state != 1) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("轮到线程一开始执行");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//线程1执行一秒
state=2;
t.notifyAll();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (t) {
if (state != 2) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("轮到线程二开始执行");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//线程1执行一秒
state=1;
t.notifyAll();
}
}
}
}).start();
}
}
打印100以内的所有数据,线程1打印奇数,打印5个数切换到线程2,线程2打印偶数,打印5个然后切换到线程1
package tf56.SpringBoot.Annotation;
/**
* Created by Administrator on 2017/10/17.
*/
public class Test1 {
private static int state = 1;
private static int num1 =1;
private static int num2 =2;
public static void main(String[] args) {
//声明一个需要加锁的对象
final Test1 t = new Test1();
//打印奇数的线程
new Thread(new Runnable() {
@Override
public void run() {
while (num1 < 100) {
synchronized (t) {
if (state != 1) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i=0; i < 5; i++) {
System.out.println(num1 + ".....");
num1+=2;
}
state = 2;
t.notifyAll();
}
}
}
}).start();
//打印偶数的线程
new Thread(new Runnable() {
@Override
public void run() {
synchronized (t){
while (num2<100){
if(state !=2){
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//打印偶数
for(int i = 0;i<5;i++){
System.out.println(num2+".....");
num2+=2;
}
state =1;
t.notifyAll();
}
}
}
}).start();
}
}
package tf56.SpringBoot.Annotation;
/**
* Created by Administrator on 2017/10/17.
*/
public class Test2 {
private static int state = 1;
public static void main(String[] args) {
final Test2 t=new Test2();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (t) {
if (state != 1) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("轮到线程一开始执行");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//线程1执行一秒
state=2;
t.notifyAll();
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
synchronized (t) {
if (state != 2) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
System.out.println("轮到线程二开始执行");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//线程1执行一秒
state=1;
t.notifyAll();
}
}
}
}).start();
}
}
打印100以内的所有数据,线程1打印奇数,打印5个数切换到线程2,线程2打印偶数,打印5个然后切换到线程1
package tf56.SpringBoot.Annotation;
/**
* Created by Administrator on 2017/10/17.
*/
public class Test1 {
private static int state = 1;
private static int num1 =1;
private static int num2 =2;
public static void main(String[] args) {
//声明一个需要加锁的对象
final Test1 t = new Test1();
//打印奇数的线程
new Thread(new Runnable() {
@Override
public void run() {
while (num1 < 100) {
synchronized (t) {
if (state != 1) {
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
for (int i=0; i < 5; i++) {
System.out.println(num1 + ".....");
num1+=2;
}
state = 2;
t.notifyAll();
}
}
}
}).start();
//打印偶数的线程
new Thread(new Runnable() {
@Override
public void run() {
synchronized (t){
while (num2<100){
if(state !=2){
try {
t.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//打印偶数
for(int i = 0;i<5;i++){
System.out.println(num2+".....");
num2+=2;
}
state =1;
t.notifyAll();
}
}
}
}).start();
}
}
相关文章推荐
- java让2个线程交替执行
- java实现让n个线程严格交替执行
- implements Runnable synchronized代码块 * 2个线程向同一数组中加随机数,每个数组加3个数,交替
- 两个线程严格交替执行java实现
- java 之 三个线程交替执行任务
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法二)
- 2个线程向同一数组中加随机数,每个数组加3个数,交替
- java两个线程交替执行
- java 中的wait和notify以及synchronized的使用,实现两个线程交替执行
- java多线程问题,线程交替执行
- java 三个线程交替执行
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数(方法一)
- implements Runnable synchronized代码块 * 2个线程向同一数组中加随机数,每个数组加3个数,交替
- Java 多线程中。两个线程交替执行,一个输出偶数,一个输出奇数
- 关于java线程的经典面试题。主线程子线程交替执行n次
- Java案例:两个线程交替执行
- 面试必问!Java 多线程中两个线程交替执行,一个输出偶数,一个输出奇数
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区
- Java【多线程知识总结(10)】线程通信之wait()与notify()的运用--模拟指挥官指挥2个连队交替轰炸战区
- 2个线程向同一数组中加随机数,每个数组加3个数,交替