Thread练习

题目:现有三个线程,分别用于打印10次A,B,C,现在要求通过代码控制,实现线程按照先后顺序打印出ABCABCABCABCABCABCABCABCABCABC


我写的代码如下:

package com.example.testabc;

public class TChar {
	private char c;
	
	public TChar(char c){
		this.c = c;
	}

	public char getC() {
		return c;
	}

	public void setC(char c) {
		this.c = c;
	}
	
}

package com.example.testabc;

public class PrintA implements Runnable{
	private TChar currentChar;
	
	public PrintA(TChar tchar){
		this.currentChar = tchar;
	}
	
	private void printChar(){
		synchronized(currentChar){
			while(currentChar.getC() != 'C'){
				try {
					currentChar.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.printf("%c", 'A');
			currentChar.setC('A');
			currentChar.notifyAll();
		}
		
	}

	@Override
	public void run() {
		for(int i=0; i<10; i++){
			printChar();
		}
	}
	
}

package com.example.testabc;

public class PrintB implements Runnable{
	private TChar currentChar;
	
	public PrintB(TChar tchar){
		this.currentChar = tchar;
	}
	
	private void printChar(){
		synchronized(currentChar){
			while(currentChar.getC() != 'A'){
				try {
					currentChar.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.printf("%c", 'B');
			currentChar.setC('B');
			currentChar.notifyAll();
		}
	}
	
	@Override
	public void run() {
		for(int i=0; i<10; i++){
			printChar();
		}
	}
}

package com.example.testabc;

public class PrintC implements Runnable{
	private TChar currentChar;
	
	public PrintC(TChar tchar){
		this.currentChar = tchar;
	}
	
	private void printChar(){
		synchronized(currentChar){
			while(currentChar.getC() != 'B'){
				try {
					currentChar.wait();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			System.out.printf("%c", 'C');
			currentChar.setC('C');
			currentChar.notifyAll();
		}
	}
	
	@Override
	public void run() {
		for(int i=0; i<10; i++){
			printChar();
		}
	}
}

package com.example.testabc;

public class TestABC{
		
	public static void main(String[] args){
		TChar cchar = new TChar('C');
		PrintA pa = new PrintA(cchar);
		PrintB pb = new PrintB(cchar);
		PrintC pc = new PrintC(cchar);
		
		Thread threadA = new Thread(pa);
		Thread threadB = new Thread(pb);
		Thread threadC = new Thread(pc);
		
		threadA.start();
		threadB.start();
		threadC.start();
		
	}
}

写得比较繁琐,希望大家可以给出自己的答案,一起分享。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,让我们来写一个简单的多线程练习吧。下面是一个使用继承 `Thread` 类的方式来创建线程的示例代码。 ```python import threading import time class MyThread(threading.Thread): def __init__(self, thread_name, delay): threading.Thread.__init__(self) self.thread_name = thread_name self.delay = delay def run(self): print('线程 %s 开始执行' % self.thread_name) for i in range(5): print('%s: %d' % (self.thread_name, i)) time.sleep(self.delay) print('线程 %s 执行完毕' % self.thread_name) if __name__ == '__main__': t1 = MyThread('线程1', 0.5) t2 = MyThread('线程2', 1) t1.start() t2.start() t1.join() t2.join() print('程序执行完毕') ``` 在上面的代码中,我们定义了一个名为 `MyThread` 的类,继承了 `threading.Thread` 类,并重写了 `run` 方法。 在 `__init__` 方法中,我们传递了 `thread_name`(线程的名称)和 `delay`(每次执行循环的延迟时间)两个参数,并在子类中使用 `self` 分别赋值给了实例变量 `self.thread_name` 和 `self.delay`。 在 `run` 方法中,我们使用 `for` 循环执行了 5 次打印输出,并在每次循环之后通过 `sleep` 方法让线程休眠一段时间。 在主函数中,我们实例化了两个线程对象 `t1` 和 `t2`,并分别启动它们的线程。最后调用 `join` 方法等待线程执行完毕,并在主线程中输出一条提示信息表示程序已经执行完毕。 当您运行上述代码时,您将看到如下输出结果: ```python 线程 线程1 开始执行 线程 线程2 开始执行 线程1: 0 线程2: 0 线程1: 1 线程2: 1 线程1: 2 线程1: 3 线程2: 2 线程1: 4 线程2: 3 线程2: 4 线程 线程1 执行完毕 线程 线程2 执行完毕 程序执行完毕 ``` 可以看出,两个线程同时执行,每个线程都按照指定的延迟时间循环执行了 5 次,并最终执行完毕。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值