程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

Java入门基础(锐格实验5)

发布于2021-05-29 23:26     阅读(1141)     评论(0)     点赞(5)     收藏(4)


在这里插入图片描述

import java.util.*;
import java.lang.Thread;
public class Main {
 public static void main(String[] args)
 {    
 	Saidao saidao=new Saidao();
 	Thread tortoise=new Thread(saidao);
	 Thread rabbit=new Thread(saidao);
 	tortoise.setName("乌龟");
	 rabbit.setName("兔子");
 	tortoise.start();
	 rabbit.start();
}
}
class Saidao implements Runnable{
	int Tdao=100, Rdao=100;
		public void run(){
		while((Tdao>0)&&(Rdao>0)){
		if(Thread.currentThread().getName().equals("乌龟")){
		Tdao-=1;
		System.out.println(Thread.currentThread().getName()+"距终点还有"+Tdao);
		}
		else if(Thread.currentThread().getName().equals("兔子")){
		Rdao-=10;
		System.out.println(Thread.currentThread().getName()+"距终点还有"+Rdao);
		if(Tdao>Rdao){
		System.out.println("我跑得快,睡一觉");
		try{Thread.sleep(1000);}
		catch(InterruptedException e){}
							}
			}
		if(Rdao==0) {System.out.println("兔子赢了");return;}
		if(Tdao==0) {System.out.println("乌龟赢了");return;}
		}
	}
}

在这里插入图片描述

import java.util.*;
import java.lang.Thread;
public class Main {
 public static void main(String[] args)
 {    
 	Maipiao maipiao=new Maipiao();
 	Thread hx=new Thread(maipiao);
	 Thread hd=new Thread(maipiao);
 	Thread hz=new Thread(maipiao);
	 hx.setName("哈西站");
 	hd.setName("哈东站");
	 hz.setName("哈站");
 	hz.start();
	 hx.start();
 	hd.start();
	}
}
class Maipiao implements Runnable{
	int TicketNum=200,n=0;
		public void run(){
		while(TicketNum>0){
		String name=Thread.currentThread().getName();
		if(name.equals("哈站")){
		sell(name);
		}
		else if(name.equals("哈西站")){
		sell(name);
		}
		else if(name.equals("哈东站")){
		sell(name);
		}							
									}
}
public synchronized void sell(String name){
	if (TicketNum==0){
	System.out.println("票已经卖完了");
	return;
	}
	n+=1;
	TicketNum-=1;
	System.out.println("现在在卖第"+n+"张票,还剩"+TicketNum+"张票");
	try{
	Thread.sleep(200);
		}
	catch(InterruptedException e){}
}
	}

在这里插入图片描述

/*public class Main {
    public static void main(String args[ ]){
        Saidao saidao=new Saidao("哈西","哈站");
    	Thread a1,a2;
    	a1=new Thread(saidao);
    	a2=new Thread(saidao);
    	a1.setName("哈西");
    	a2.setName("哈站");
    	a1.start();
    	a2.start();
	}
}
class Saidao implements Runnable{
	String s1,s2;
	Saidao(String s1,String s2){
		this.s1=s1;
		this.s2=s2;
	}
	public void run() {
		int distance=100;
		int count=0;
		while(true) {
			if(Thread.currentThread().getName().equals(s1)) {
				distance-=1;
				count++;
				System.out.println(s1+"已经卖了"+count+"还有"+distance);
				try {
					Thread.sleep(200);
				}catch(InterruptedException e) {}
				if(distance==0) {
					System.out.println(s1+"的票已经卖完了");
					return;//结束当前线程
				}
			}
			else if(Thread.currentThread().getName().equals(s2)) {
				distance-=1;
				count++;
				System.out.println(s2+"已经卖了"+count+"还有"+distance);
				try {
					Thread.sleep(200);
				}catch(InterruptedException e) {}
				if(distance==0) {
					System.out.println(s2+"的票已经卖完了");
					return;//结束当前线程
				}
			}
		}
	}
}*/
public class Main {
    public static void main(String args[ ]){
        Maipiao a1=new Maipiao("哈西");
    	Maipiao a2=new Maipiao("哈站");
    	a1.start();
    	a2.start();
	}
}
class Maipiao extends Thread{
	private static int Ticketnum=100,count=0;
	String s;
	Maipiao(String s){
		this.s=s;
	}
	public void run() {
				while(true) {
					Ticketnum--;
					count++;
					System.out.println(s+"现在已经卖了"+count+"还剩"+Ticketnum);
					try {
						Thread.sleep(200);
					}catch(InterruptedException e) {}
					if(Ticketnum==0) {
						System.out.println(s+"的票已经卖完啦!");
						return;
					}
				}
				
		}
}

在这里插入图片描述

import java.util.*;
import java.lang.Thread;
public class Main {
 public static void main(String[] args)
 {    
     Maipiao maipiao=new Maipiao();
 	Thread hx=new Thread(maipiao);
 	Thread hz=new Thread(maipiao);
	 hx.setName("哈西站");
	 hz.setName("哈站");
 	hz.start();
	 hx.start();
	}
}
class Maipiao implements Runnable{
	int TicketNum=100,n=0;
		public void run(){
		while(TicketNum>0){
		String name=Thread.currentThread().getName();
		if(name.equals("哈站")){
		sell(name);
		}
		else if(name.equals("哈西站")){
		sell(name);
		}					
									}
}
public synchronized void sell(String name){
	if (TicketNum==0){
	System.out.println("票已经卖完了");
	return;
	}
	n+=1;
	TicketNum-=1;
	System.out.println("现在在卖第"+n+"张票,还剩"+TicketNum+"张票");
	try{
	Thread.sleep(200);
		}
	catch(InterruptedException e){}
}
	}


所属网站分类: 技术文章 > 博客

作者:niceboty

链接:http://www.javaheidong.com/blog/article/207823/a05d348a80ba09516539/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)