面试题:启动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();
		
	}

}

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值