Java n個線程輪流打印數字的問題



一. 實現兩個線程。輪流打印出數字。例如以下:

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類的方法比較easy理解。 lock() 和 unlock()之間的塊是被鎖定的


用synchronize的方法少用了個flag來標志輪到哪個線程來打印,這是由於線程鎖的notifity( )方法會釋放鎖並喚醒其它線程 ,線程鎖的wait( )方法則是釋放鎖並休眠當前線程,假設剛好僅僅有兩個線程,那么自然就是開始還有一個線程而休眠本線程。


二.擴展到n個線程輪流打印數字的問題


n個線程輪流打印數字。用Lock類是比較easy擴展的


比方三個線程輪流打印數字

package com.myexample.test;


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

public class ThreeThreadPrinter {

	private Lock threadLock = new ReentrantLock();

	private int flag = 0;
	
	int count =10;

	Thread aThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 鎖定
					threadLock.lock();
					try {
						if ( count < 1) {
							return;
						}
						if (count%3 == 0 ) {
							// aThread的任務
							System.out.println("aThread --> " + count);
							count--;
						}
					} 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 (count%3 == 1 ) {
							// aThread的任務
							System.out.println("bThread --> " + count);
							count--;
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						// 釋放鎖
						threadLock.unlock();
				}
			}
		}
	});

	Thread cThread = new Thread(new Runnable() {
		public void run() {
			while (true) {
					// 鎖定
					threadLock.lock();
					try {
						if ( count < 1) {
							return;
						}
						if (count%3 == 2 ) {
							// aThread的任務
							System.out.println("cThread --> " + count);
							count--;
						}
					} catch (Exception e) {
						// TODO: handle exception
					} finally {
						// 釋放鎖
						threadLock.unlock();
				}
			}
		}
	});
	
	public void startTwoThread() {
		aThread.start();
		bThread.start();
		cThread.start();
	}

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

}

輸出結果

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




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM