码迷,mamicode.com
首页 > 编程语言 > 详细

Java-两个线程轮流打印数字的问题

时间:2015-08-19 20:47:36      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:线程锁


实现两个线程,轮流打印出数字,如下:

bThread --> 10
aThread --> 9
bThread --> 8
aThread --> 7
bThread --> 6
aThread --> 5
bThread --> 4
aThread --> 3
bThread --> 2
aThread --> 1

用java中的Lock类实现:

package com.yjq.thread_demo;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TwoThreadPrinter {

	private Lock threadLock = new ReentrantLock();

	private boolean flag = false;
	
	int count =10;

	Thread aThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 锁定
					threadLock.lock();
					try {
						if ( count < 1) {
							return;
						}
						if (flag) {
							// aThread的任务
							System.out.println("aThread --> " + (count--));
							flag = !flag;
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						// 释放锁
						threadLock.unlock();
					}
				}
		}
	});

	Thread bThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 锁定
					threadLock.lock();
					try {
						if ( count < 1) {
							return;
						}
						if (!flag) {
							// aThread的任务
							System.out.println("bThread --> " + (count--));
							flag = !flag;
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						// 释放锁
						threadLock.unlock();
				}
			}
		}
	});

	public void startTwoThread() {
		aThread.start();
		bThread.start();
	}

	public static void main(String[] args) {
		TwoThreadPrinter twoThreadPrinter = new TwoThreadPrinter();
		twoThreadPrinter.startTwoThread();
	}

}


用synchronized实现:

package com.yjq.thread_demo;

public class TwoThreadPrinter2 {
	

	
private Object threadLock = new Object();

	int count =10;

	Thread aThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 锁定
					synchronized (threadLock) {
						if ( count < 1) {
							return;
						}

//							// aThread的任务
							System.out.println("aThread --> " + (count--));

						threadLock.notify();
						try {
							threadLock.wait();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
		}
	});

	Thread bThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 锁定
					synchronized (threadLock) {
						if ( count < 1) {
							return;
						}

//							// aThread的任务
							System.out.println("bThread --> " + (count--));

						threadLock.notify();
						try {
							threadLock.wait();
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}
		}
	});

	public void startTwoThread() {
		aThread.start();
		bThread.start();
	}

	public static void main(String[] args) {
		TwoThreadPrinter twoThreadPrinter = new TwoThreadPrinter();
		twoThreadPrinter.startTwoThread();
	}

}


用Lock类的方法比较容易理解, lock() 和 unlock()之前的块是被锁定的


用synchronize的方法少用了个flag来标志轮到哪个线程来打印,这是因为线程锁的notifity( )方法会释放锁并唤醒其他线程 ,线程锁的wait( )方法则是释放锁并休眠当前线程




版权声明:本文为博主原创文章,未经博主允许不得转载。

Java-两个线程轮流打印数字的问题

标签:线程锁

原文地址:http://blog.csdn.net/guige_csdn/article/details/47782633

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!