Java 线程 复习题

本文介绍了如何使用Java线程联合,通过`ReadWrite`类实现了读线程和写线程的协作,确保了读写操作的同步与高效。使用`Thread.join()`方法让读线程等待写线程执行完毕再继续,展示了多线程间的交互和同步机制。
摘要由CSDN通过智能技术生成
import java.util.*;

class RWthread {
	public static void main(String args[]) {
		//作为实参
		String readName = "读线程", writeName = "写线程";
		//实现了Runnable接口的对象
		ReadWrite rw = new ReadWrite(readName, writeName);
		Thread read, write;
		read = new Thread(rw);		//Thread类有参构造方法
		write = new Thread(rw);
		//起名字
		read.setName(readName);
		write.setName(writeName);
		//排队等待CPU的资源
		read.start();
		write.start();
	}
}

class ReadWrite implements Runnable {
	String ID = null;
	String name = null;
	String readName, writeName;
	boolean flag = false;
	public ReadWrite(String s1, String s) {
		readName = s1;
		writeName = s;
	}
	//run()调用同步方法
	public void run() {
		readOrWrite(); 
	}
	
	//同步方法 关键字synchronized
	public synchronized void readOrWrite() {
		//判断当前线程对象
		if(Thread.currentThread().getName().equals(readName)) {
			//读线程
			Scanner sc = new Scanner(System.in);
			while(true) {
				if(flag) {
					try {
						wait();		//等待,让出CPU的使用权
					} catch(InterruptedException e) {}
				}
				System.out.println("请输入学号:");
				ID = sc.nextLine();		//接收学号
				if(ID.equals("finish")) {
					System.out.println("\n读线程和写线程工作结束!");
					flag = true;
					notify();		//结束等待
					sc.close();
					return;		//消亡
				}
				System.out.println("请输入姓名:");
				name = sc.nextLine();
				flag = true;
				notify();		//通知另一个正在等待的线程结束等待
			}
		}
		//判断当前线程
		else if(Thread.currentThread().getName().equals(writeName)) {
			//写线程
			while(true) {
				if(!flag) {		//如果先抢到资源的是写线程,执行wait()
					try {
						wait();
					} catch(InterruptedException e) {}
				}
				if(ID.equals("finish")) {
					return;		//消亡
				}
				System.out.println("\n输出学号:"+ID + ",输出姓名:"+name);
				System.out.println();
				flag = false;
				notify();
			}
		}
	}  
}

改为用线程联合实现:

import java.util.*;

class RWthread {
	public static void main(String args[]) {
		ReadWrite rw = new ReadWrite();
		rw.read.start();
	}
}

class ReadWrite implements Runnable {  
	Thread read, write;
	String ID, name;
	public ReadWrite() {
		//Thread类有参构造方法
		read = new Thread(this);
	}
	public void run() { 
		Scanner sc = new Scanner(System.in);
		//判断当前抢到CPU资源的是谁
		if(Thread.currentThread() == read) {  
			while(true) {
				System.out.println("请输入学号:");
				ID = sc.nextLine();
				if(ID.equals("finish")) {
					System.out.println("\n读线程和写线程工作结束!");
					return;
				}
				System.out.println("请输入姓名:");
				name = sc.nextLine();
				write = new Thread(this);  
				write.start();
				try {
					write.join();		//read联合write,CPU资源让给write
					//等待write运行完,继续执行reader
				} catch(InterruptedException e) {}
			}
		}
		else if(Thread.currentThread() == write) {    
			System.out.println("\n输出学号:"+ID+",输出姓名:"+name);
			System.out.println();
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值