我是靠谱客的博主 雪白毛豆,最近开发中收集的这篇文章主要介绍面试题:启动3个线程打印递增的数字.........,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

附加题---需要使用synchronized和lock两种方式来完成
1:启动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

解题思路:先定义一个int数,默认为1,表示从1开始,接着定义一个标识,来控制锁对象 ,利用分离的思想创建三个方法。

synchronized方式

public class PrintNum {
public int num=1;//从1开始打印
public int isPrint=1;//标识,1,2,3中标识分别代表三种状态
//synchronized方式
//方法一
标识1
public synchronized void fun1() {
while (isPrint!=1) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=2;
notifyAll();
}
//方法二
标识2
public synchronized void fun2() {
while (isPrint!=2) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=3;
notifyAll();
}
//方法三 标识3
public synchronized void fun3() {
while (isPrint!=3) {
try {
wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=1;
notifyAll();
}
public void printXunHuan() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+":"+num++);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import com.qf.bean.PrintNum;
public class PrintNumTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintNum printNum=new PrintNum();
//线程1
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printNum.num<=65) {
printNum.fun1();
}
}
}, "线程1").start();
//线程2
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printNum.num<=70) {
printNum.fun2();
}
}
}, "线程2").start();
//线程3
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printNum.num<=75) {
printNum.fun3();
}
}
}, "线程3").start();
}
}

lock方式

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class PrintNumLock {
private int num=1;//从1开始
private int isPrint=1;//标识
1,2,3
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public PrintNumLock() {
super();
}
final Lock lock=new ReentrantLock();
final Condition oneCondition=lock.newCondition();
final Condition twoCondition=lock.newCondition();
final Condition threeCondition=lock.newCondition();
//锁1
public void funOne() {
lock.lock();
try {
while (isPrint!=1) {
try {
oneCondition.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=2;//切换标识2
唤醒锁2
twoCondition.signal();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
//锁2
public void funTwo() {
lock.lock();
try {
while (isPrint!=2) {
try {
twoCondition.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=3;//切换标识3
唤醒锁3
threeCondition.signal();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
//锁3
public void funThree() {
lock.lock();
try {
while (isPrint!=3) {
try {
threeCondition.await();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
printXunHuan();
isPrint=1;//切换为标识1
唤醒锁1
oneCondition.signal();
} finally {
// TODO: handle finally clause
lock.unlock();
}
}
//控制循环5次
public void printXunHuan() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+":"+num++);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import com.qf.bean.PrintNumLock;
public class PrintNumLockTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintNumLock printnum=new PrintNumLock();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printnum.getNum()<=65) {
printnum.funOne();
}
}
}, "线程1").start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printnum.getNum()<=70) {
printnum.funTwo();
}
}
}, "线程2").start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while (printnum.getNum()<=75) {
printnum.funThree();
}
}
}, "线程3").start();
}
}

本题还有简单的解法 ,就是把三个方法合并为一个进行控制。大家有兴趣可以试试。。。。。

最后

以上就是雪白毛豆为你收集整理的面试题:启动3个线程打印递增的数字.........的全部内容,希望文章能够帮你解决面试题:启动3个线程打印递增的数字.........所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(33)

评论列表共有 0 条评论

立即
投稿
返回
顶部