第十一章编程作业

1). 设计一个多线程的程序如下:设计一个火车售票模拟程序。假如火车站要有100张火车票要卖出,现在有5个售票点同时售票,用5个线程模拟这5个售票点的售票情况。

package cn.whb.test11;

public class Work1 {
    
    

	public static void main(String[] args) {
    
    
		SoleToken s = new SoleToken(100);
		new Thread(s,"窗口一").start();
		new Thread(s,"窗口二").start();
		new Thread(s,"窗口三").start();
		new Thread(s,"窗口四").start();
		new Thread(s,"窗口五").start();
	}

}

class SoleToken implements Runnable {
    
    
	
	private int tokenCount;

	
	public SoleToken(int tokenCount) {
    
    
		this.tokenCount = tokenCount;
	}

	public void run() {
    
    
		
		while(tokenCount>0) {
    
    
			SoleVotes();
		}
	}
	
	private void SoleVotes()  {
    
    
		
		if(tokenCount<=0) {
    
    
			return;
		}
		synchronized(this) {
    
    			
			if(tokenCount<=0) {
    
    
				return;
			}
			
			try {
    
    
				Thread.sleep(100);
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"出售了第"+(tokenCount--)+"张票");
		}
	}
}

2). 编写两个线程,一个线程打印1-52的整数,另一个线程打印字母A-Z。打印顺序为12A34B56C….5152Z。即按照整数和字母的顺序从小到大打印,并且每打印两个整数后,打印一个字母,交替循环打印,直到打印到整数52和字母Z结束。

要求

1 . 编写打印类Printer,声明私有属性index,初始值为1,用来表示是第几次打印。

2 . 在打印类Printer中编写打印数字的方法print(int i),3的倍数就使用wait()方法等待,否则就输出i,使用notifyAll()进行唤醒其它线程。

3 . 在打印类Printer中编写打印字母的方法print(char c),不是3的倍数就等待,否则就打印输出字母c,使用notifyAll()进行唤醒其它线程。

4 . 编写打印数字的线程NumberPrinter继承Thread类,声明私有属性private Printer p;在构造方法中进行赋值,实现父类的run方法,调用Printer类中的输出数字的方法。

5 . 编写打印字母的线程LetterPrinter继承Thread类,声明私有属性private Printer p;在构造方法中进行赋值,实现父类的run方法,调用Printer类中的输出字母的方法。

6 . 编写测试类Test,创建打印类对象,创建两个线程类对象,启动线程。

package cn.whb.test11;

public class Test {
    
    

	public static void main(String[] args) {
    
    
		Printer p = new Printer();
		
		new NumberPrinter(p).start();
		new LetterPrinter(p).start();
		
	}

}
class Printer{
    
    
	private int index=1;
	
	public synchronized void print(int i) {
    
    
		if(index%3==0) {
    
    
			try {
    
    
				wait();
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
		System.out.print(i);
		index++;
		if(index%3==0) {
    
     // 防止本来还是输出数字,但却把输出字母的线程唤醒
			notifyAll();
		}
	}
	
	public synchronized void print(char c) {
    
    
		if(index%3!=0) {
    
    
			try {
    
    
				wait();
			} catch (InterruptedException e) {
    
    
				e.printStackTrace();
			}
		}
		System.out.print(c);
		index++;
		notifyAll();
	}
}

class NumberPrinter extends Thread{
    
    
	
	public NumberPrinter(Printer p) {
    
    
		this.p = p;
	}


	private Printer p;
	
	
	public void run() {
    
    
		for(int i=1;i<=52;i++) {
    
    
			p.print(i);
		}
	}
}

class LetterPrinter extends Thread{
    
    
	private Printer p;
	
	public LetterPrinter(Printer p) {
    
    
		this.p = p;
	}

	public void run() {
    
    
		for(int i=0;i<26;i++) {
    
    
			p.print((char)('A'+i));;
		}
		System.out.println();
	}
}

3). 编写多线程程序,模拟多个人通过一个山洞的模拟。这个山洞每次只能通过一个人,每个人通过山洞的时间为5秒,有10个人同时准备过此山洞,显示每次通过山洞人的姓名和顺序。

package cn.whb.test11;

public class Work3 {
    
    

	public static void main(String[] args) {
    
    
		
		CrossCave cave =  new CrossCave();
		
		for(int i=1;i<=10;i++) {
    
    
			new Thread(cave,"游客"+i).start();
		}
	}

}
class CrossCave implements Runnable{
    
    
	
	private int index=1;
	
	public void run() {
    
    
		
		cross();
	}
	
	private synchronized void cross() {
    
    
		System.out.println("第"+(index++)+"个通过山洞的人为"+Thread.currentThread().getName());
		try {
    
    
			Thread.sleep(5000);
		} catch (InterruptedException e) {
    
    
			e.printStackTrace();
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_46456049/article/details/109411265