java程序设计基础_陈国君版第五版_第十一章例题

java程序设计基础_陈国君版第五版_第十一章习题
class MyThread extends Thread
{	
	private String who;
	public MyThread(String who)
	{
		this.who = who;
	}
	public void run()
	{
		for(int i =0 ; i<5; i++)
		{
			try
			{
				sleep((int)(1000*Math.random()));
			}
			catch(InterruptedException e)
			{}
			System.out.println(who+"正在运行!!");
		}
	}
}
public class Main11_1 {
	public static void main(String[] args){
		MyThread you = new MyThread("你");
		MyThread she = new MyThread("她");
		you.start();
		she.start();
		System.out.println("主方法main()运行结束!");
	}
}

class MyThread2 implements Runnable
{
	private String who;
	public MyThread2(String who)
	{
		this.who = who;
	}
	public void run()
	{
		for(int i =0 ;i<5;i++)
		{
			try
			{
				Thread.sleep((int)(1000*Math.random()));
			}
			catch(InterruptedException e)
			{
				System.out.println(e.toString());
			}
			System.out.println(who+"正在运行!!");
		}
	}
}
public class Main11_2 {
	public static void main(String[] args)
	{
		MyThread you = new MyThread("你");
		MyThread she = new MyThread("她");
		Thread t1 = new Thread(you);
		Thread t2 = new Thread(she);
		t1.start();
		t2.start();
	}
}

class MyThread3 extends Thread
{	
	private String who;
	public MyThread3(String who)
	{
		this.who = who;
	}
	public void run()
	{
		for(int i =0 ; i<5; i++)
		{
			try
			{
				sleep((int)(1000*Math.random()));
			}
			catch(InterruptedException e)
			{}
			System.out.println(who+"正在运行!!");
		}
	}
}
public class Main11_3 {
	public static void main(String[] args)
	{
		MyThread3 you = new MyThread3("你");
		MyThread3 she = new MyThread3("她");
		you.start();
		try {
			you.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		she.start();
		try {
			she.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("主方法main()运行结束!!");
	}
}

class ThreadSale extends Thread
{
	private int tickets = 10;
	public void run()
	{
		while(true)
		{
			if(tickets>0)
				System.out.println(getName()+"   售机票第"+tickets--+"号");
			else
				System.exit(0);
		}
	}
}
public class Main11_4 {
	public static void main(String[] args)
	{
		ThreadSale t1 = new ThreadSale();
		ThreadSale t2 = new ThreadSale();
		ThreadSale t3 = new ThreadSale();
		t1.start();
		t2.start();
		t3.start();
	}
}

class ThreadSale5 implements Runnable
{
	private int tickets = 10;
	public void run()
	{
		while(true)
		{
			if(tickets>0)
				System.out.println(Thread.currentThread().getName()+"售机票第"+tickets--+"号");
			else
				System.exit(0);
		}
	}
}
public class Main11_5 {
	public static void main(String[] args){
		ThreadSale5 t = new ThreadSale5();
		Thread t1 = new Thread(t,"第1售票窗口");
		Thread t2 = new Thread(t,"第2售票窗口");
		Thread t3 = new Thread(t,"第3售票窗口");
		t1.start();
		t2.start();
		t3.start();
	}
}

class Mbank
{
	private static int sum = 2000;
	public static void take(int k)
	{
		int temp = sum;
		temp -= k;
		try
		{
			Thread.sleep((int)(1000*Math.random()));
		}
		catch(InterruptedException e)
		{}
		sum =temp;
		System.out.println("sum ="+sum);
	}
}
class Customer extends Thread
{
	public void run()
	{
		for(int i = 1;i<=4;i++)
			Mbank.take(100);
	}
}
public class Main11_6 {
	public static void main(String[] args){
		Customer c1 = new Customer();
		Customer c2 = new Customer();
		c1.start();
		c2.start();
	}
}

class Mbank7
{
	private static int sum = 2000;
	public synchronized static void take(int k)
	{
		int temp = sum;
		temp -= k;
		try
		{
			Thread.sleep((int)(1000*Math.random()));
		}
		catch(InterruptedException e)
		{}
		sum =temp;
		System.out.println("sum ="+sum);
	}
}
class Customer7 extends Thread
{
	public void run()
	{
		for(int i = 1;i<=4;i++)
			Mbank7.take(100);
	}
}
public class Main11_7 {
	public static void main(String[] args){
		Customer7 c1 = new Customer7();
		Customer7 c2 = new Customer7();
		c1.start();
		c2.start();
	}
}

public class Main11_8 {
	public static void main(String[] args){
		Tickets t = new Tickets(10);
		new Producer(t).start();
		new Consumer(t).start();
	}
}
class Tickets
{
	protected int size;
	int number = 0 ;
	boolean available = false;
	public Tickets(int size)
	{
		this.size = size;
	}
	public synchronized void put()
	{
		if(available)
			try{wait();}
			catch(Exception e){}
		System.out.println("存入第【"+(++number)+"】号票");
		available = true;
		notify();
	}
	public synchronized void sell()
	{
		if(!available)
			try{wait();}
			catch(Exception e){}
		System.out.println("售出第【"+(number)+"】号票");
		available = false;
		notify();
		if(number == size)  number =size+1;
	}
}
class Producer extends Thread
{
	Tickets t = null;
	public Producer(Tickets t)
	{
		this.t = t;
	}
	public void run()
	{
		while(t.number < t.size)
			t.put();
	}
}
class Consumer extends Thread
{
	Tickets t = null;
	public Consumer(Tickets t)
	{
		this.t = t;
	}
	public void run()
	{
		while(t.number <= t.size)
			t.sell();
	}
}

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值