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();
}
}
解决方案
您需要 Thread 的“run()”方法。或者您可以将方法重命名go
为run
static class ExtendThread extends Thread {
@Override
public void run() {
this.go();
}
}
推荐阅读
- php - 从分块数组中获取值
- python - “网络浏览器”模块如何工作?
- php - 是否可以在不创建服务器的情况下在 NodeJS 中获取 POST 数据?
- android - 如何解决这些不匹配问题?
- python-2.7 - 单选按钮不会选择 jpg
- php - Prestashop 1.6.0.9 与 php 5.6
- css - CssLint - 意外的令牌
- java - dataSnapshot 检索到最后一条消息,但难以在小部件上显示它......?
- c# - .net core 2.1 测试 API 控制器缺少 AspNetCore.Mvc 参考
- data-structures - 如何在 Rust HashMap 中为同一个键存储多个元素?