两个线程循环切换

package com.thread;

/**
 * 创建两个线程,一个线程循环10次,另一个线程100次,两者交替运行50次
 * 即到A线程时,它运行本身循环的10次之后;马上轮到B线程,它则运行本身的100次循环
 * 以此轮换切换线程共50次
 */
public class ThreadTest {
	public static void main(String[] args) {
		
		final Business business=new Business();

		
		//子线程
		new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 50; i++) {
					try {
						business.sub(i);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
		
//----------------------------线程二-------------------------------------------------
		
		//main方法本身是个线程,即线程二运行代码直接放到main方法中
		for (int i = 0; i < 50; i++) {
			try {
				business.main(i);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}
}

class Business{
	private boolean isSub=true;//当前运行的方法是sub()
	
	//线程一
	public synchronized void sub(int i) throws InterruptedException {
		while (!isSub) {
			this.wait();
		}
		for (int j = 0; j < 10; j++) {
			System.out.println("sub thread sequence is "+j+" and loop is "+i);
		}
		isSub = false;
		this.notify();
	}
	
	//主线程,即main
	public synchronized void main(int i) throws InterruptedException{
		while (isSub) {
			this.wait();
		}
		for (int j = 0; j < 100; j++) {
			System.out.println("main thread sequence is "+j+" and loop is "+i);
		}
		isSub = true;
		this.notify();
	}
}

 

### 回答1: ``` class PrinterThread extends Thread { private String name; public PrinterThread(String name) { this.name = name; } public void run() { for (int i = 0; i < 10; i++) { System.out.println(name + ": " + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } public class Main { public static void main(String[] args) { PrinterThread t1 = new PrinterThread("Thread 1"); PrinterThread t2 = new PrinterThread("Thread 2"); t1.start(); t2.start(); } } ``` 该代码创建了两个线程`PrinterThread t1`和`PrinterThread t2`,并在`run`方法中实现了循环打印。每个线程在每次循环打印后休眠100毫秒,以让另一个线程有机会执行。 ### 回答2: 可以使用Java的多线程机制来实现两个线程循环打印的功能。以下是示例代码: ```java public class LoopPrinting { private static final Object lock = new Object(); private static int num = 1; private static int count = 1; public static void main(String[] args) { Thread threadA = new Thread(new PrintThread("A", 1)); Thread threadB = new Thread(new PrintThread("B", 2)); threadA.start(); threadB.start(); } static class PrintThread implements Runnable { private String threadName; private int threadNumber; public PrintThread(String threadName, int threadNumber) { this.threadName = threadName; this.threadNumber = threadNumber; } @Override public void run() { while (num <= 100) { synchronized (lock) { if (count % 3 == threadNumber - 1) { System.out.println(threadName + ":" + num); num++; count++; lock.notifyAll(); } else { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } } } } ``` 在上述代码中,定义了一个共享的锁对象`lock`,一个共享的计数变量`num`和一个判断打印顺序的变量`count`。两个线程`ThreadA`和`ThreadB`通过`PrintThread`类实例化,并传入不同的线程名和线程编号。 在`PrintThread`的`run`方法中,使用`synchronized`关键字锁定`lock`对象,保证在每个线程执行时只有一个线程能够访问共享资源。通过判断`count`变量的值,实现两个线程循环打印。其中`count % 3`的结果等于`threadNumber`减一,表示该线程可以进行打印操作。 当线程打印完一个数字后,更新`num`和`count`的值,并使用`lock.notifyAll()`唤醒其他正在等待的线程。然后当前线程释放锁并进入等待状态,直到其他线程唤醒它。 ### 回答3: 可以使用Java中的多线程编程来实现两个线程循环打印。以下是一个简单的示例代码: ```java public class TwoThreadPrinting { private static final Object lock = new Object(); private static volatile boolean isThread1Turn = true; // 标识线程1是否应该打印 public static void main(String[] args) { Thread thread1 = new Thread(new PrintThread(1)); Thread thread2 = new Thread(new PrintThread(2)); thread1.start(); thread2.start(); } private static class PrintThread implements Runnable { private final int threadNumber; public PrintThread(int threadNumber) { this.threadNumber = threadNumber; } @Override public void run() { try { for (int i = 1; i <= 10; i++) { synchronized (lock) { // 如果当前线程不是轮到自己打印,则当前线程等待 while ((threadNumber == 1 && !isThread1Turn) || (threadNumber == 2 && isThread1Turn)) { lock.wait(); } System.out.println("Thread " + threadNumber + ": " + i); isThread1Turn = !isThread1Turn; // 切换下一个线程的打印权限 lock.notifyAll(); // 唤醒所有等待的线程 } } } catch (InterruptedException e) { e.printStackTrace(); } } } } ``` 在上述代码中,我们定义了一个`TwoThreadPrinting`类,并在该类中创建了两个线程对象`thread1`和`thread2`。每个线程都被分配了一个`PrintThread`实例,其中`PrintThread`实现了`Runnable`接口,并且重写了`run`方法。在`run`方法中,通过`synchronized`关键字和`wait`、`notifyAll`方法来实现线程的同步与等待。 在程序运行过程中,每个线程都会进行循环打印10次,打印的内容格式为`Thread x: y`,其中`x`表示线程编号,`y`表示打印的数字。线程1在打印完当前数字后,会将`isThread1Turn`设置为`false`,然后唤醒线程2;而线程2在打印完当前数字后,会将`isThread1Turn`设置为`true`,然后唤醒线程1。这样两个线程就能够循环打印了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值