发布于2021-05-29 23:26 阅读(1317) 评论(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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!