栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

两个线程交替打印输出的各种写法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

两个线程交替打印输出的各种写法

方式一:使用LockSupport
package com.muluo.test1013;

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


public class TodayCodeTest {
	static String str1 = "abc";
	static String str2 = "123";
	static Thread t1 = null;
	static Thread t2 = null;
	public static void main(String[] args) {
		t1 = new Thread(() -> {
			for (int i = 0; i < str2.length(); i++) {
				System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
				LockSupport.unpark(t2); // 唤醒线程
				LockSupport.park(); // 挂起当前线程
			}
		}, "t1");
		
		t2 = new Thread(() -> {
			for (int i = 0; i < str1.length(); i++) {
				LockSupport.park();
				System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
				LockSupport.unpark(t1);
			}
		}, "t2");
		
		t1.start();
		t2.start();
		
	}
方式二 wait/notify/ 标志位flag
package com.muluo.test1013;

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


public class TodayCodeTest {
	static String str1 = "abc";
	static String str2 = "123";
	boolean flag = false;
	public static void main(String[] args) {
		TodayCodeTest todayCodeTest = new TodayCodeTest();
		new Thread(() -> {
			todayCodeTest.print123();
		}, "t1");
		
		new Thread(() -> {
			todayCodeTest.printabc1();
		}, "t2");
	}

	public synchronized void printabc1() {

		for (int i = 0; i < str1.length(); i++) {
			try {
				while (flag) {
					wait();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + str1.charAt(i));
			notify();
			flag = true;
		}
	}

	public synchronized void print123() {
		for (int i = 0; i < str2.length(); i++) {
			try {
				while (!flag) {
					wait();
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName() + str2.charAt(i));
			notify();
			flag = false;
		}
	}
	
方式三 使用Object的notify和wait
package com.muluo.test1013;

import java.util.concurrent.locks.LockSupport;


public class NotifyAndWaitAndObject {
	public static void main(String[] args) {
		String str1 = "abc";
		String str2 = "123";
		Object obj = new Object();
		
		new Thread(() -> {
			synchronized(obj) {
				for (int i = 0; i < str2.length(); i++) {
					System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
					obj.notify();
					try {
						obj.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}, "t1").start();
		
		new Thread(() -> {
			synchronized(obj) {
				for (int i = 0; i < str1.length(); i++) {
					System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
					obj.notify();
					try {
						obj.wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}, "t2").start();
	}
}

方式四
package com.muluo.test1013;

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


public class ReentrantLockAndCondition {
	public static void main(String[] args) {
		// 1a+2b+3c+
		String str1 = "123";
		String str2 = "abc";
		String str3 = "+++";
		
		Lock lock = new ReentrantLock();
		Condition con1 = lock.newCondition();
		Condition con2 = lock.newCondition();
		Condition con3 = lock.newCondition();

		new Thread(() -> {
			lock.lock();
			for (int i = 0; i < str1.length(); i++) {
				System.out.println(Thread.currentThread().getName() + " " + str1.charAt(i));
				try {
					con2.signal(); // 唤醒
					con1.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			lock.unlock();
		}, "t1").start();

		new Thread(() -> {
			lock.lock();
			for (int i = 0; i < str2.length(); i++) {
				System.out.println(Thread.currentThread().getName() + " " + str2.charAt(i));
				try {
					con3.signal();
					con2.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			}
			lock.unlock();
		}, "t2").start();
		
		new Thread(() -> {
			lock.lock();
			for (int i = 0; i < str3.length(); i++) {
				System.out.println(Thread.currentThread().getName() + " " + str3.charAt(i));
				try {
					con1.signal();
					con3.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} 
			} 
			lock.unlock();
		}, "t3").start();
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/322461.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号