用javawon';行不通
我是Java新手,我编写了这个竞赛程序。我试图运行它,但它在Race和RaceMain类中给出了一个非法的监视器状态异常错误。。。你能帮忙吗用javawon';行不通,java,multithreading,Java,Multithreading,我是Java新手,我编写了这个竞赛程序。我试图运行它,但它在Race和RaceMain类中给出了一个非法的监视器状态异常错误。。。你能帮忙吗 public class RaceMain { public static void main(String args[]) throws InterruptedException{ Pitstop p = new Pitstop(); Race r = new Race(); new Ca
public class RaceMain {
public static void main(String args[]) throws InterruptedException{
Pitstop p = new Pitstop();
Race r = new Race();
new Car("Ryan Hunter", r, p);
new Car("Takumo Sato", r, p);
new Car("Scott Dixon", r, p);
new Car("Sebastien Bourdais", r, p);
r.StartRace();
}
}
public class Race {
int rank = 0;
void getToStart() throws InterruptedException{
wait();
}
void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
synchronized void FinishRace(Car c){
rank = rank +1;
System.out.println("<<<<<<" + c.name + " finished " + geth(rank) + ">>>>>>");
}
String geth(int i){
switch(i % 10){
case 1:
return i + "st";
case 2:
return i + "nd";
case 3:
return i +"rd";
default:
return i + "th";
}
}
}
public class Car implements Runnable{
String name;
Race r;
Pitstop p;
int rand;
int lapsCompleted;
boolean needRep = false;
Car(String n, Race r, Pitstop p){
name = n;
this.r = r;
this.p = p;
}
public void run(){
try{
r.getToStart();
System.out.println(name + " is off and speeding!");
for(int i = 1; i < 10; i++){
if(needRep){
p.repair(name);
needRep = false;
}
runLap();
}
}catch(InterruptedException e){
System.out.println(e);
}
}
void runLap() throws InterruptedException{
rand = (int)(Math.random() % 500) + 1000;
Thread.sleep(rand);
System.out.println(name + " completed his " + geth(lapsCompleted) + " lap");
if(Math.floor(Math.random()*10) == 5){
System.out.println("Oh snap! Looks like " + name + "'s tire has got bursted during the race!");
needRep = true;
}
}
String geth(int i){
switch(i % 10){
case 1:
return i + "st";
case 2:
return i + "nd";
case 3:
return i +"rd";
default:
return i + "th";
}
}
}
public class Pitstop {
synchronized void repair(String n) throws InterruptedException{
int dur = (int) (Math.random() * 1000) % 1000 + 5000;
Thread.sleep(dur);
double sec = dur / 1000;
System.out.println(n + " stopped for " + sec);
}
}
public类RaceMain{
公共静态void main(字符串args[])引发InterruptedException{
Pitstop p=新Pitstop();
种族r=新种族();
新车(“Ryan Hunter”,r,p);
新车(“Takumo Sato”,r,p);
新车(“斯科特·迪克森”,r,p);
新车(“Sebastien Bourdais”,r,p);
r、 星迹();
}
}
公共级别比赛{
int秩=0;
void getostart()引发InterruptedException{
等待();
}
void StartRace()引发InterruptedException{
睡眠(1000);
notifyAll();
}
同步空隙精加工面(c车){
秩=秩+1;
System.out.println(“”);
}
字符串geth(int i){
开关(i%10){
案例1:
返回i+“st”;
案例2:
返回i+“nd”;
案例3:
返回i+“rd”;
违约:
返回i+“th”;
}
}
}
公共级轿车可运行{
字符串名;
小种r;
PITP;
国际兰特;
int lapsCompleted;
布尔needRep=false;
赛车(n线、r线、p线){
name=n;
这个。r=r;
这个,p=p;
}
公开募捐{
试一试{
r、 getostart();
System.out.println(name+“关闭并超速!”);
对于(int i=1;i<10;i++){
if(needRep){
p、 修理(名称);
needRep=false;
}
runLap();
}
}捕捉(中断异常e){
系统输出打印ln(e);
}
}
void runLap()引发InterruptedException{
rand=(int)(Math.random()%500)+1000;
睡眠(兰德);
System.out.println(name+“completed his”+geth(lapsCompleted)+“lap”);
如果(数学地板(数学随机()*10)==5){
System.out.println(“哦,突然!看起来“+name+”的轮胎在比赛中爆了!”);
needRep=true;
}
}
字符串geth(int i){
开关(i%10){
案例1:
返回i+“st”;
案例2:
返回i+“nd”;
案例3:
返回i+“rd”;
违约:
返回i+“th”;
}
}
}
公营停车场{
同步无效修复(字符串n)引发InterruptedException{
int dur=(int)(Math.random()*1000)%1000+5000;
睡眠(dur);
双秒=dur/1000;
系统输出打印项次(n+“停止”+秒);
}
}
您有两种方法是需要同步的竞赛代码的一部分:
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
这两种方法都调用了其他仅在同步块内有效的方法:
- :抛出IllegalMonitorStateException-如果当前线程不是对象监视器的所有者
- 抛出IllegalMonitorStateException-如果当前线程不是此对象监视器的所有者
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
您的竞赛代码中有两种方法需要同步:
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
这两种方法都调用了其他仅在同步块内有效的方法:
- :抛出IllegalMonitorStateException-如果当前线程不是对象监视器的所有者
- 抛出IllegalMonitorStateException-如果当前线程不是此对象监视器的所有者
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
您的竞赛代码中有两种方法需要同步:
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
这两种方法都调用了其他仅在同步块内有效的方法:
- :抛出IllegalMonitorStateException-如果当前线程不是对象监视器的所有者
- 抛出IllegalMonitorStateException-如果当前线程不是此对象监视器的所有者
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
您的竞赛代码中有两种方法需要同步:
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
这两种方法都调用了其他仅在同步块内有效的方法:
- :抛出IllegalMonitorStateException-如果当前线程不是对象监视器的所有者
- 抛出IllegalMonitorStateException-如果当前线程不是此对象监视器的所有者
synchronized void getToStart() throws InterruptedException{
wait();
}
synchronized void StartRace() throws InterruptedException{
Thread.sleep(1000);
notifyAll();
}
似乎您想要运行多个线程,因为
Car
实现了Runnable
这将是正确的方法:
Pitstop p = new Pitstop();
Race r = new Race();
new Thread(new Car("Ryan Hunter", r, p)).start();
new Thread(new Car("Takumo Sato", r, p)).start();
new Thread(new Car("Scott Dixon", r, p)).start();
new Thread(new Car("Sebastien Bourdais", r, p)).start();
r.StartRace();
现在,就这两种方法而言,我将完全删除它们。首先是因为从错误的线程错误地调用了它们,其次是因为我看不出它们的用途:
void getToStart() throws InterruptedException{
// remove this
}
void StartRace() throws InterruptedException{
// remove this
}
现在可以在run方法中添加线程。yield
,如下所示:
public void run() {
try {
System.out.println(name + " is off and speeding!");
for (int i = 1; i < 10; i++) {
if (needRep) {
p.repair(name);
needRep = false;
}
runLap();
Thread.yield();
}
} catch (InterruptedException e) {
System.out.println(e);
}
似乎您想要运行多个线程,因为
Car
实现了Runnable
这将是正确的方法:
Pitstop p = new Pitstop();
Race r = new Race();
new Thread(new Car("Ryan Hunter", r, p)).start();
new Thread(new Car("Takumo Sato", r, p)).start();
new Thread(new Car("Scott Dixon", r, p)).start();
new Thread(new Car("Sebastien Bourdais", r, p)).start();
r.StartRace();
现在,就这两种方法而言,我将完全删除它们。首先是因为从错误的线程错误地调用了它们,其次是因为我看不出它们的用途:
void getToStart() throws InterruptedException{
// remove this
}
void StartRace() throws InterruptedException{
// remove this
}
现在可以在run方法中添加线程。yield
,如下所示:
public void run() {
try {
System.out.println(name + " is off and speeding!");
for (int i = 1; i < 10; i++) {
if (needRep) {
p.repair(name);
needRep = false;
}
runLap();
Thread.yield();
}
} catch (InterruptedException e) {
System.out.println(e);
}
似乎您想要运行多个线程,因为
Car
实现了Runnable
这将是正确的方法:
Pitstop p = new Pitstop();
Race r = new Race();
new Thread(new Car("Ryan Hunter", r, p)).start();
new Thread(new Car("Takumo Sato", r, p)).start();
new Thread(new Car("Scott Dixon", r, p)).start();
new Thread(new Car("Sebastien Bourdais", r, p)).start();
r.StartRace();
现在,就这两种方法而言,我将完全删除它们。首先是因为从错误的线程错误地调用了它们,其次是因为我看不出它们的用途:
void getToStart() throws InterruptedException{
// remove this
}
void StartRace() throws InterruptedException{
// remove this
}
现在可以在run方法中添加线程。yield
,如下所示:
public void run() {
try {
System.out.println(name + " is off and speeding!");
for (int i = 1; i < 10; i++) {
if (needRep) {
p.repair(name);
needRep = false;
}
runLap();
Thread.yield();
}
} catch (InterruptedException e) {
System.out.println(e);
}
似乎您想要运行多个线程,因为
Car
实现了Runnable
这将是正确的方法:
Pitstop p = new Pitstop();
Race r = new Race();
new Thread(new Car("Ryan Hunter", r, p)).start();
new Thread(new Car("Takumo Sato", r, p)).start();
new Thread(new Car("Scott Dixon", r, p)).start();
new Thread(new Car("Sebastien Bourdais", r, p)).start();
r.StartRace();
现在,就这两种方法而言,我将删除