实现交替控制线程

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

 

你可能感兴趣的:(实现交替控制线程)