实验六学习日记

5 篇文章 0 订阅

1.通过继承Thread类的方法创建两个线程,在Thread构造方法中指定线程的名称分别为“Thread_one”和”Thread_two”;其中一个线程向控制台输出奇数数列(1、3、5、7、……、19),时间间隔0.5秒;另一个线程向控制台输出偶数数列(2、4、6、8、……、20),时间间隔0.3秒。

最后,将输出奇数数列线程设置为守护线程,观察一下运行结果会有什么变化。

package src6;

public class xiancheng1{
	   public static void main(String []args)
	   {
		 
		   Threadone one =new Threadone();
		   Threadtwo two =new Threadtwo();
		     one.setDaemon(true);
				   one.start();
				   two.start();
				                     
	   }
		
	}


	class Threadone extends Thread
	{
	  public void run()
	  {
		for(int i=1;i<=20;i++)
		{
			if(i%2!=0)
			{
				
			System.out.println(i);
			
			}
			
			try {
					sleep(500);
			}
			
			catch(InterruptedException e)
			{ }
		}
	  }
	}

	class Threadtwo extends Thread
	{
		public void run()
		{
		for(int i=1;i<=20;i++)
		{
			if(i%2==0)
			{
				
			System.out.println(i);
			
			}
			
			try {
					sleep(300);
			}
			
			catch(InterruptedException e)
			{ }
		}
		
	  }
		
	}
}

2通过实现Runnable接口的方法创建一个新线程,要求main线程在控制台输出10行“main”,时间间隔0.2秒;新线程在控制台输出5行“new”,时间间隔0.4秒。

package src6;

public class xiancheng2 implements Runnable {
	public void run()
	{
		for(int i=0;i<10;i++)
		{
			System.out.println("main");
			try
			{
				Thread.sleep(200);
			}catch(InterruptedException e)
			{}
		}
		for(int i=0;i<5;i++)
		{
			System.out.println("new");
                 try
                 {
                	Thread.sleep(400);
                 }catch(InterruptedException e)
     			{}
		}
	}
	    
	public static void main(String []args)
	   {
		   xiancheng2 test =new xiancheng2();
		   new1 test1 =new new1();
		   Thread one =new Thread(test);
		   Thread two =new Thread(test1);
		   one.start();
		   two.start();
	   }
}

 class new1 implements Runnable 
{
         public void run()
         {
        	 for(int i=0;i<5;i++)
     		{
     			System.out.println("new");
                      try
                      {
                     	Thread.sleep(400);
                      }catch(InterruptedException e)
          			{}
         }
   }
}

3. 下列程序代码是模拟火车售票情况,开启两个窗口销售10张火车票,程序一次运行结果如图1所示。程序运行有错,请改正。

package src6;

/*public class SaleTicketThread extends Thread {
	private String name;
	private  static int tickets = 10;
	private  String lock="";	

	public SaleTicketThread(String name) { 	this.name = name;	}

	@Override
	public synchronized void  run() {
		while (true) {		
				if (tickets > 0)
					System.out.println(name + ":正在销售第 " + tickets-- + " 票");
				else  		break;			
			try { 	Thread.sleep(500);	}
catch (InterruptedException e) {	}
		}
	}

	
	public static void main(String[] args) {
		SaleTicketThread sale1 = new SaleTicketThread("窗口1");
		SaleTicketThread sale2 = new SaleTicketThread("窗口2");
		sale1.start();
		sale2.start();
	   }
	
}
*/
public class SaleTicketThread implements Runnable {
	private String name;
	private  static int tickets = 10;
	private  String lock="";	

	public SaleTicketThread(String name) { 	this.name = name;	}

	@Override
	public  void  run() {
		while (true) {		
				if (tickets > 0)
					System.out.println(name + ":正在销售第 " + tickets-- + " 票");
				else  		break;			
			try { 	Thread.sleep(500);	}
catch (InterruptedException e) {	}
		}
	}

	
	public static void main(String[] args) {
		SaleTicketThread one =new SaleTicketThread("窗口1");
		SaleTicketThread two =new SaleTicketThread("窗口2");
		Thread t1 =new Thread(one);
		Thread t2 =new Thread(two);
		t1.start();
		t2.start();
	   }
}

4有一个抽奖池,该抽奖池中存放了9个奖金的金额,该抽奖池用一个数组 arr = {10, 20, 50, 100, 200, 500, 800, 80, 300}表示。

假设抽奖池有三个抽奖口(一个线程模拟一个口),设置其名称分别为“抽奖口1”、“抽奖口2”、“抽奖口3”,程序模拟随机抽奖,并在控制台上输出如图2所示的结果。
在这里插入图片描述

package src6;
public class Lottery implements Runnable {
	int[] arr = { 10, 20, 50, 100, 200, 500, 800, 80, 300 };
int num = arr.length;
// 记录对应的奖券是否抽过,为true表示已抽过、为false表示未抽过
boolean[] flag = new boolean[arr.length];

	@Override
	public   void  run() {
		while (true) {
			synchronized (this) {
				if (num > 0) {  
					// 产生指定范围(与奖券下标相一致)内的随机数
				int index = (int) (Math.random() * arr.length-1);
				int get = arr[index];
				if (flag[index] != true) {	
					try {	Thread.sleep(500);	} 
					catch (Exception e) {	}
					flag[index] = true;
					System.out.println(Thread.currentThread().getName() + 
							" 抽出一个" + get + "元大奖");
					num--;
					
				}
			}
			else break;			
		}
		
			}
			

	}

	public static void main(String[] args) {
		Lottery target=new Lottery();
		Thread th1=new Thread(target,"抽奖池1:");
		Thread th2=new Thread(target,"抽奖池2:");
		Thread th3=new Thread(target,"抽奖池3:");
		th1.start();
		th2.start();
		th3.start();
		
	}
}
  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值