Java入门基础(锐格实验5)
Posted 晨沉宸辰
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java入门基础(锐格实验5)相关的知识,希望对你有一定的参考价值。
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){}
}
}
以上是关于Java入门基础(锐格实验5)的主要内容,如果未能解决你的问题,请参考以下文章