-1 java
该作业与在桥上行驶的汽车有关,它们向东和向西行驶。应该可以避免死锁。我需要知道的是如何实际显示它,因为它正在编译和技术上运行(只是不显示任何内容)。我目前将所有内容都保存在一个文件中。需要在某个地方分开吗?我不知道。
import java.util.concurrent.*;
public class Cars {
//creating a static class to create variables
static class ExtendThread extends Thread {
//create variables
Semaphore sem;
String name;
//creating a public class for the ExtendThread
public ExtendThread(Semaphore sem, String name) {
super(name);
//creating instance variables for the current class
this.sem = sem;
this.name = name;
}
//creating an area to run the simulation
public void go() {
//creating an if statement for the westbound cars
if (this.getName().equals("Westbound Cars")) {
//creating a try to make sure there are no interrupted exceptions
try {
//print statement for who is waiting
System.out.println(name + " are waiting to cross.");
//acquiring the semaphore
sem.acquire();
//print statement for who is attempting to cross
System.out.println(name = " are attempting to cross.");
//a for statement to allow individual westbound cars to cross or wait more
for (int i = 1; i < 9; i++) {
//print statement to show which car(from 1-9) is still waiting
System.out.println("Westbound car " + i + " is waiting to cross.");
Thread.sleep(1000);
//print statement to show which car(from 1-9) has crossed
System.out.println("Westbound car " + i + " has crossed.");
Thread.sleep(1000);
}
}
//catch portion for the try-catch statement
catch (InterruptedException exc) {
System.out.println(exc);
}
//print statement for who has crossed the bridge
System.out.println(name + " have crossed the bridge.");
//releasing the semaphore
sem.release();
}
//creating an else statement for the eastbound cars
else {
//print statement for readibility
System.out.println(name + " are waiting to cross the bridge.");
//creating a try to make sure there are no interrupted exceptions
try {
//print statement for who is waiting
System.out.println(name + " is waiting to cross.");
//acquiring the semaphore
sem.acquire();
//print statment for who is attempting to cross
System.out.println(name + " is attempting to cross.");
//a for statement to allow individual eastbound cars to cross or wait more
for (int i = 1; i < 9; i++) {
//print statement to show which car(from 1-9) is still waiting
System.out.println("Eastbound car " + i + " is waiting to cross.");
Thread.sleep(1000);
//print statement to show which car(from 1-9) has crossed
System.out.println("Eastbound car " + i + " has crossed.");
Thread.sleep(1000);
}
}
//catch portion for the try-catch statement
catch (InterruptedException exc) {
System.out.println(exc);
}
//print statement for who has crossed the bridge
System.out.println(name + " have crossed the bridge.");
//releasing the semaphore
sem.release();
}
}
}
//creating the main class
public static void main(String [] args) throws InterruptedException {
//creating a semaphore object with a single permit
Semaphore sem = new Semaphore(1);
//creating the cars
ExtendThread exTh1 = new ExtendThread(sem,"Westbound Cars");
ExtendThread exTh2 = new ExtendThread(sem,"Eastbound Cars");
//running the program
exTh1.start();
exTh2.start();
exTh1.join();
exTh2.join();
}
}
Run Code Online (Sandbox Code Playgroud)
您需要线程的“run()”方法。或者您可以将该方法重命名go为run
static class ExtendThread extends Thread {
@Override
public void run() {
this.go();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57 次 |
| 最近记录: |