三个线程顺序循环执行的3种方式

本文介绍了三种实现打印ABC循环的方法,包括使用Object的wait和notifyAll方法、利用lock和Condition以及通过concurrent的信号量Semaphore。每种方法都有详细的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方式:使用Object的wait和notifyAll方法

package printABC.method1;

//第一种方法,使用Object的wait和notifyAll方法
public class TestPrint {
	static int count = 0;
	static final Object obj = new Object();
	Thread t1 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 0) {
						System.out.println("A");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});
	Thread t2 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 1) {
						System.out.println("B");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});
	Thread t3 = new Thread(new Runnable() {
		@Override
		public void run() {
			while (true) {
				synchronized (obj) {
					if (count % 3 == 2) {
						System.out.println("C");
						count++;
						obj.notifyAll();
					} else
						try {
							obj.wait();
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
				}
			}
		}
	});

	public void fun() {
		t3.start();
		t1.start();
		t2.start();
	}

	public static void main(String[] args) {
		TestPrint tp = new TestPrint();
		long t1 = System.currentTimeMillis();
		tp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)// 运行10个毫秒
				System.exit(0);
		}
	}
}

第二种方法:使用lock和condition

package printABC.method2;

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

//使用lock和Condition,更加灵活
public class LockPrint {
	private static int count = 0;
	private Lock lock = new ReentrantLock();
	Condition c1 = lock.newCondition();
	Condition c2 = lock.newCondition();
	Condition c3 = lock.newCondition();
	Thread t1 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 0)
						c1.await();
					System.out.println("A");
					count++;
					c2.signal();// 唤醒条件2
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});
	Thread t2 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 1)
						c2.await();
					System.out.println("B");
					count++;
					c3.signal();// 唤醒条件3
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});
	Thread t3 = new Thread(new Runnable() {

		@Override
		public void run() {
			while (true) {
				try {
					lock.lock();
					while (count % 3 != 2)
						c3.await();
					System.out.println("C");
					count++;
					c1.signal();// 唤醒条件1
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					lock.unlock();
				}
			}

		}
	});

	public void fun() {
		t3.start();
		t1.start();
		t2.start();
	}

	public static void main(String[] args) {
		LockPrint lp = new LockPrint();
		long t1 = System.currentTimeMillis();
		lp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)
				System.exit(0);
		}
	}
}

第三种方法:使用concurrent的信号量Semaphore

package printABC.method3;

import java.util.concurrent.Semaphore;

//使用信号量
public class ConcurrentPrint {
	// 共享资源个数都初始为1
	private static Semaphore s1 = new Semaphore(1);
	private static Semaphore s2 = new Semaphore(1);
	private static Semaphore s3 = new Semaphore(1);
	Thread t1 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s1.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("A");
				s2.release();
			}
		}
	});
	Thread t2 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s2.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("B");
				s3.release();
			}
		}
	});
	Thread t3 = new Thread(new Runnable() {
		public void run() {
			while (true) {
				try {
					s3.acquire();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				System.out.println("C");
				s1.release();
			}
		}
	});

	public void fun() throws InterruptedException {
		// 先占有输出BC的线程的信号量计数
		// 则只能从输出A的线程开始。获取信号量A,然后释放B-获取B-释放C-获取C-释放A,由此形成循环
		s2.acquire();
		s3.acquire();
		t2.start();
		t3.start();
		t1.start();
	}

	public static void main(String[] args) throws InterruptedException {
		ConcurrentPrint cp = new ConcurrentPrint();
		long t1 = System.currentTimeMillis();
		cp.fun();
		while (true) {
			if (System.currentTimeMillis() - t1 >= 10)
				System.exit(0);
		}
	}
}

 

大模型国产替代是指在人工智能领域,国内自主研发和应用大规模预训练模型,以替代依赖于国外的大型模型。目前,国内已经有一些企业和研究机构在这方面取得了一定的进展。 首先,国内已经有一些大规模预训练模型的研发和应用。例如,百度推出的ERNIE(Enhanced Representation through Knowledge Integration)模型,腾讯推出的BERT(Bidirectional Encoder Representations from Transformers)模型等。这些模型在自然语言处理、文本生成等任务上取得了不错的效果。 其次,国内也在积极探索自主研发大规模预训练模型的路径。例如,中科院计算所推出的GPT(Generative Pre-trained Transformer)模型,通过自主研发和优化,取得了与国外同类模型相媲美的效果。此外,还有一些企业和研究机构在自主研发大规模预训练模型方面进行了一系列的尝试和实践。 然而,要实现真正的大模型国产替代,还面临一些挑战。首先是数据集的问题,大规模预训练模型需要大量的数据进行训练,而国内的数据集相对于国外来说还有一定的差距。其次是计算资源的问题,训练大规模模型需要庞大的计算资源,包括高性能的计算机和大规模的分布式训练平台。此外,还需要解决模型的可解释性、隐私保护等问题。 总体来说,大模型国产替代是可行的,但需要在数据集、计算资源、算法研发等方面进行持续投入和努力。通过国内企业、研究机构和政府的共同努力,相信在不久的将来,国内将能够自主研发和应用更多的大规模预训练模型。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值