一个面试很大几率碰到的代码题,也挺有意思,就写下来做下笔记。
package com.xiaoxuesheng.service;
/**
* @author: xiaoxuesheng
* @description:
* @date: 19/3/12
*/
/**
* 要求创建三个线程,输出1-75,
* 最开始第一个线程输出1-3,第二个输出4-6,第三个输出7-9
* 接着再第一个线程输出10-12...就这样循环下去,直到打印出75个数
* 思路:1-75,每个线程打印3次,1,2/3=0 3,4,5/3=1 6,7,8/3=3 9,10,11/3=3 ......
0%3 = 0 1%3 = 1 2%3 = 2 3%3 = 0 4%3 = 1 5%3 = 2 6%3 = 0 ......
所以n/3%3=0 让线程1打印,n/3%3=1 让线程2打印,n/3%3=3 让线程3打印,维护好n的递增就可以了
*/
public class Test {
static class Printer implements Runnable{
static int num = 1; //开始数字
static final int END = 75;
int id;
public Printer(int id) {
this.id = id;
}
public void run(){
synchronized (Printer.class) {
while(num <= END){
if(num / 3 % 3 == id){ //如果是属于自己的数,依次打印出来三个
for(int i = 0; i < 3; i++){
System.out.print(num++ + ", ");
}
Printer.class.notifyAll();//放弃CPU使用权,唤醒等待在Print.class队列上的的打印线程
}else{
try {
Printer.class.wait();//如果不属于自己的数,把当前线程挂在Printer.class这个对象的等待队列上(也是放弃CPU使用权),等待唤醒
} catch (InterruptedException e) {
System.out.println("id" + "被打断了");
}
}
}
}
}
}
public static void main(String[] args) {
//下面可以不按0,1,2的顺序来,而且在两两中间随便sleep(),都会正确打印出来
new Thread( new Printer(0)).start();
new Thread( new Printer(1)).start();
new Thread( new Printer(2)).start();
// 打印下面注释掉的代码,即可看到规律
// int a = 1;
// while (a < 76){
// System.out.println(a / 3 % 3);
// a++;
// }
}
}