一道经典的Java多线程编程题

问题描述
启动3个线程打印递增的数字, 线程1先打印1,2,3,4,5, 然后是线程2打印6,7,8,9,10, 然后是线程3打印11,12,13,14,15. 接着再由线程1打印16,17,18,19,20….以此类推, 直到打印到75. 程序的输出结果应该为:

线程1: 1
线程1: 2
线程1: 3
线程1: 4
线程1: 5

线程2: 6
线程2: 7
线程2: 8
线程2: 9
线程2: 10

线程3: 71
线程3: 72
线程3: 73
线程3: 74
线程3: 75

解法一:

class PrintRunnable implements Runnable {

private static volatile int printNum = 0;
private int threadId;
private Object o;
public PrintRunnable(int threadId,Object o){
    this.threadId = threadId;
    this.o=o;
}
@Override
public void run() {
    while(printNum < 75){
        synchronized (o){
            if (printNum/5%3 + 1 == threadId){
            //该判断确保对应线程输出
                for (int i = 0; i <5; i++) {
                    System.out.println("线程"+threadId+":"+(++printNum));
                }
                o.notifyAll();
            }else {
                try {
                    o.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

}
public class Main {
public static void main(String[] args) throws InterruptedException {
Object o = new Object();
new Thread(new PrintRunnable(1,o)).start();
new Thread(new PrintRunnable(2,o)).start();
new Thread(new PrintRunnable(3,o)).start();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
解法二:

public class Print123456 implements Runnable{
private String name;
private Object prev;
private Object self;
private int count;
public Print123456(String name,Object prev,Object self,int count){
this.name=name;
this.prev=prev;
this.self=self;
this.count=count;
}
@Override
public void run() {
while(count<=75){
synchronized (prev){
synchronized (self){
for (int i=0;i<5;i++){
count=count+1;
if(count>75)break;
System.out.println(name+”: “+count);

                }
                count=count+10;
                //System.out.println("释放自身锁,并唤醒在等待该锁的线程");
                self.notify();//释放自身锁,并唤醒在等待该锁的线程

            }
            //System.out.println("释放自身锁成功");
            try {
                //System.out.println("等待前一个锁");
                prev.wait();//等待前一个锁

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        //System.out.println("两个锁全部释放,重新进入新的循环,但是由于有prev.wait()所以该线程等待获取prev的锁");
    }
}

}
public class Main {
public static void main(String[] args) throws InterruptedException {
Object a=new Object();
Object b=new Object();
Object c=new Object();
Print123456 A=new Print123456(“线程1”,c,a,0);
Print123456 B=new Print123456(“线程2”,a,b,5);
Print123456 C=new Print123456(“线程3”,b,c,10);
new Thread(A).start();
Thread.sleep(100);
new Thread(B).start();
Thread.sleep(100);
new Thread(C).start();
Thread.sleep(100);
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值