1. 程式人生 > >實現交替控制執行緒

實現交替控制執行緒

1.實現如下結果12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q3536R3738S3940T4142U4344V4546W4748X4950Y5152Z

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

/**
 * 
 * @author jakie
 * 實現效果
 *12A34B56C78D910E1112F1314G1516H1718I1920J2122K2324L2526M2728N2930O3132P3334Q
 *3536R3738S3940T4142U4344V4546W4748X4950Y5152Z
 */
class MyQueue1 {
	private Lock lock = new ReentrantLock();
	private int flag = 1;
	private int number = 1;
	private char letter = 65;
	private Condition c1 = lock.newCondition();
	private Condition c2 = lock.newCondition();

	public void printNumber() {
		lock.lock();
		try {
			while (flag != 1) {
				c1.await();
			}
			for (int i = 0; i < 2; i++) {
				System.out.print(number);
				number++;
			}
			flag = 2;
			c2.signal();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}

	public void printLetter() {
		lock.lock();
		try {
			while (flag != 2) {
				c2.await();
			}
			System.out.print(letter++);

			flag = 1;
			c1.signal();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			lock.unlock();
		}

	}
}

public class ThreadExer7 {
	public static void main(String[] args) {
		MyQueue1 mq = new MyQueue1();
		new Thread(() -> {
			for (int i = 1; i <= 26; i++) {
				mq.printNumber();
			}

		}).start();

		new Thread(() -> {
			for (int i = 1; i <= 26; i++) {
				mq.printLetter();
			}

		}).start();
	}
}

2.實現交替列印執行緒名稱各10次,例如tes1,test2,test1,test2,test1,test2...

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

/**
 * 
 * @author jakie
 *
 */
class ShareThread{
	//執行緒切換標誌
	private int flag = 1;//1 ->Test1  2->Test2
	//鎖
	private Lock lock = new ReentrantLock();
	//喚醒或使執行緒掛起
	private Condition c1 = lock.newCondition();
	private Condition c2 = lock.newCondition();
	
	public void printA(int totalLoop)
	{
		lock.lock();
		try 
		{
			//1 判斷
			while(flag != 1)
			{
				c1.await();
			}
			//2 幹活
				System.out.println(Thread.currentThread().getName()+"\t"+"\t totalLoop: "+totalLoop);
			//3 通知喚醒
			flag = 2;
			c2.signal();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
	}
	
	public void printB(int totalLoop)
	{
		lock.lock();
		try 
		{
			//1 判斷
			while(flag != 2)
			{
				c2.await();
			}
			//2 幹活
				System.out.println(Thread.currentThread().getName()+"\t"+"\t totalLoop: "+totalLoop);
			//3 通知喚醒
			flag = 1;
			c1.signal();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			lock.unlock();
		}
	}	
}
/**
 * 交替列印執行緒名稱A 10次  B 10次
 * @author jakie
 *
 */
public class TestThread {
	public static void main(String[] args) {
		ShareThread sr = new ShareThread();
		//設定A名稱,並啟動執行緒
		new Thread(() ->
		{
			for (int i = 1; i <=10; i++) 
			{
				sr.printA(i);
			}
		}, "Test1").start();
		//設定B,並啟動執行緒
		new Thread(() ->
		{
			for (int i = 1; i <=10; i++) 
			{
				sr.printB(i);
			}
		}, "Test2").start();		
	}
}

 產生結果如下:

Test1		 totalLoop: 1
Test2		 totalLoop: 1
Test1		 totalLoop: 2
Test2		 totalLoop: 2
Test1		 totalLoop: 3
Test2		 totalLoop: 3
Test1		 totalLoop: 4
Test2		 totalLoop: 4
Test1		 totalLoop: 5
Test2		 totalLoop: 5
Test1		 totalLoop: 6
Test2		 totalLoop: 6
Test1		 totalLoop: 7
Test2		 totalLoop: 7
Test1		 totalLoop: 8
Test2		 totalLoop: 8
Test1		 totalLoop: 9
Test2		 totalLoop: 9
Test1		 totalLoop: 10
Test2		 totalLoop: 10