Java 9.10习题

<1>设计4个线程对象,两个线程执行减操作,两个线程执行加操作

//=================================================
// File Name       :	Thread4_demo
//------------------------------------------------------------------------------
// Author          :	Common

// 类名:Operator
// 属性:
// 方法:
class Operator{
	
	private static int i;
	
	// 内部类名:Inc
	// 属性:
	// 方法:
	class Inc implements Runnable{
		
		@Override
		public void run() {
			// TODO 自动生成的方法存根
			for(int j=0;j<10;j++){
				this.inc();
				System.out.println(Thread.currentThread().getName()+",i="+i);
			}
		}
		
		public synchronized void inc(){
			i++;
		}
		
	}
	
	//内部类名:Dec
	//属性:
	//方法:
	class Dec implements Runnable{
		
		@Override
		public void run() {
			// TODO 自动生成的方法存根
			for(int j=0;j<10;j++){
				this.dec();
				System.out.println(Thread.currentThread().getName()+",i="+i);
			}
		}
		
		public synchronized void dec(){
			i--;
		}
	}
}


//主类
//Function        : 	ThreadStop_demo
public class Thread4_demo {
	
	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Operator.Inc inc1 = new Operator().new Inc();		//实例化内部类对象
		Operator.Inc inc2 = new Operator().new Inc();		//实例化内部类对象
		Operator.Dec dec1 = new Operator().new Dec();		//实例化内部类对象
		Operator.Dec dec2 = new Operator().new Dec();		//实例化内部类对象
		
		Thread t1 = new Thread(inc1);							//实例化Thread类对象
		Thread t2 = new Thread(inc2);							//实例化Thread类对象
		Thread t3 = new Thread(dec1);							//实例化Thread类对象
		Thread t4 = new Thread(dec2);							//实例化Thread类对象
		
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}

}

<2>设计一个生产电脑和搬运电脑类,要求生产出一台电脑就搬走一台电脑,如果没有新的电脑生产出来,则搬运工要等待新电脑产出;如果生产出的电脑没有搬走,则要等待电脑搬走之后再生产,并统计出生产的电脑数量。

//=================================================
// File Name       :	computer_transfer_demo
//------------------------------------------------------------------------------
// Author          :	Common

//类名:Computer
//属性:
//方法:
class Computer {
	private String name = "未生产";
	private boolean flag = true;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public synchronized void set(String name){	//设置信息名称及内容
		if(!flag){																	//标志位为false,不可以生产,在这里等待取走
			try{
				super.wait();												//等待搬运者取走
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}	
		this.setName(name);								//设置信息名称	
		System.out.println(this.getName());	//输出信息
		try{
			Thread.sleep(300);									//加入延迟
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		flag = false;													//标志位为true,表示可以取走
		super.notify();											//唤醒等待线程
	}
	
	public synchronized void get(){			//取得信息内容
		if(flag){															//标志位为true,不可以取走
			try{
				super.wait();										//等待生产者生产
			}catch(InterruptedException e){
				e.printStackTrace();
			}
		}		
		try {
			Thread.sleep(300);									//加入延迟
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		this.setName("已经搬运完毕");
		System.out.println(this.getName());	//输出信息
		flag = true;													//修改标志位为true,表示可以生产
		super.notify();											//唤醒等待线程
	}
	
}

//类名:producer
//属性:
//方法:
class producer implements Runnable{		//定义生产者线程

	private Computer com = null;								//保存Computer引用
	
	public producer(Computer com) {						//构造函数
		super();
		this.com = com;
	}

	@Override
	public void run() {
		int count = 0;
		// TODO 自动生成的方法存根
		for(int i=0;i<10;i++){
			this.com.set("已经生产完毕");
			count++;
		}
		System.out.println("生产的电脑数量:"+count);
	}
	
}

//类名:transfer
//属性:
//方法:
class transfer implements Runnable{		//定义生产者线程

	private Computer com = null;								//保存Computer引用
	
	public transfer(Computer com) {						//构造函数
		super();
		this.com = com;
	}

	@Override
	public void run() {
		// TODO 自动生成的方法存根
		for(int i=0;i<10;i++){
			this.com.get();
			
		}
	}
	
}

//主类
//Function        : 	computer_transfer_demo
public class computer_transfer_demo {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
		Computer c = new Computer();
		producer pro = new producer(c);
		transfer tra = new transfer(c);
		new Thread(pro).start();
		new Thread(tra).start();
	}

}
原文地址:https://www.cnblogs.com/tonglin0325/p/5254958.html