java作业 进程 同步

package thread;

 class Coutalphabet extends Thread{
	public void run() {
		for(int i = 'A';i<='Z';i++)
		{
			char c = (char)i;
			System.out.print(" "+c);
		}
	}
}
  class CoutGreekalphabet {
	public void run() {
		for(int i = 'α';i<='α'+24;i++ )
		{
			char c = (char)i;
			System.out.print(" "+c);
		}
	}
}
		 class first {
			public static void main(String args[]) {
				Coutalphabet coutalphabet;
				CoutGreekalphabet coutgreekalphabet;
				coutalphabet = new Coutalphabet();
				coutalphabet.start();
				coutgreekalphabet = new CoutGreekalphabet();	
				coutgreekalphabet.run();
			}
}
package tt;
 class ClassRoom implements Runnable{
	 Thread teacher;
	 Thread student1 ;
	 Thread student2 ;
	ClassRoom(){
	teacher = new Thread(this);
	student1 = new Thread(this);
	student2 = new Thread(this);
	teacher.setName("老师");
	student1.setName("李四");
	student2.setName("王五");
	}

	@Override
	public void run() {
		if(Thread.currentThread() == teacher)
		{
				try {Thread.sleep(100);
						for(int i=1 ;i <=3;i++)
						{
							System.out.println("上课!");
						}
						
		}
				catch(InterruptedException e)
				{
					
				}
				student1.interrupt();
		}
		if(Thread.currentThread() == student1)
		{
				try {
					
							System.out.println("我李四要睡10分钟的觉,睡觉真香!");	
								Thread.sleep(1000*60*10);
		}
				catch(InterruptedException e)
				{
					System.out.println("我李四竟然被老师吵醒了,悄咪咪我也把王五叫起来哈哈哈!");		
				}
				student2.interrupt();
		}
		if(Thread.currentThread() == student2)
		{
				try {
							System.out.println("我王五要睡李四的五倍,睡觉香!");	
								Thread.sleep(1000*60*50);
		}
				catch(InterruptedException e)
				{
					System.out.println("李四竟然把我吵醒了!啊,原来要上课了!");	
				}
		}
	}
}
public class first {
		public static void main(String args[]) throws InterruptedException
		{
			ClassRoom classroom = new ClassRoom();
			classroom.teacher.start();
			classroom.student1.start();
			classroom.student2.start();
		}
}

package Afirst;
class Coutalphabet extends Thread{
     public void run(){
        if(Thread.currentThread().getName().equals("coutalphabet"))
{
	print();
}
  else if(Thread.currentThread().getName().equals("coutgreekalphabet")) {
	  
	  print();
        }
     }
	public  synchronized void print() {
		  if(Thread.currentThread().getName().equals("coutalphabet"))
		  {  for(int i = 'A';i<='Z';i++)
			{
				char c = (char)i;
				System.out.print(" "+c);
			}
			  
		  }
		  else if(Thread.currentThread().getName().equals("coutgreekalphabet"))
		  {
			  try {
				  Thread.sleep(100);
				  for(int i = 'α';i<='α'+24;i++ )
				{
					char c = (char)i;
					System.out.print(" "+c);
				}	 
			  }
			  catch(InterruptedException e) {	  
			  }
		  }
	}
    
}
public class Afirst {
	    public static void main(String args[]) throws InterruptedException
	 {	 
	Coutalphabet coutalphabet = new Coutalphabet();
	Coutalphabet coutgreekalphabet =new Coutalphabet();
	coutalphabet.setName("coutalphabet");
	coutgreekalphabet.setName("coutgreekalphabet");
		coutalphabet.start();
		coutgreekalphabet.start();
		
	}	
}

package Aticket;

class TicketHouse implements Runnable {

	int moneyfive = 1;
	int moneyten = 0;
	int moneytwenty = 0;

	@Override

	public void run() {
		if (Thread.currentThread().getName().equals("张某")) {
			saleTicket(20);
		} else if (Thread.currentThread().getName().equals("孙某")) {
			saleTicket(10);
		} else if (Thread.currentThread().getName().equals("赵某")) {
			saleTicket(5);
		}
	}

	public synchronized void saleTicket(int money) {
		if (money == 10 && moneyfive >= 1) {
			moneyten += 1;
			try {
				Thread.sleep(100);
				System.out.println("给" + Thread.currentThread().getName() + "入场券" + "!还给您5元");
			} catch (InterruptedException e) {

			}
		} else if (money == 5) {
			moneyfive += 1;
			try {
				Thread.sleep(50);
				System.out.println("给" + Thread.currentThread().getName() + "入场券" + ",钱刚好够");

			} catch (InterruptedException e) {

			}
		} else if (money == 20) {
			while ( moneyten < 1&&money>=2) {
				try {
					System.out.println("\n" + Thread.currentThread().getName() + "靠边等...");
					wait();
					System.out.println("\n" + Thread.currentThread().getName() + "继续买票");
				} catch (InterruptedException e) {
				}
			}
			if (moneyfive >= 3 || moneyten >= 1) {
				moneyfive -= 3;
				moneytwenty += 1;
				System.out.println("给" + Thread.currentThread().getName() + "入场券," + Thread.currentThread().getName()
						+ "给20,找回15元");
			}
		}
	notifyAll();
	}
}
public class Aticket {
	public static void main(String args[]) {

		TicketHouse officer = new TicketHouse();
		Thread Sun = new Thread(officer);
		Thread Zhang = new Thread(officer);
		Thread Zhao = new Thread(officer);
		Sun.setName("孙某");
		Zhang.setName("张某");
		Zhao.setName("赵某");
		Sun.start();
		Zhang.start();
		Zhao.start();
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值