Java 两线程共享数据的多线程编程
我应该制作一个程序,模拟汽车试图通过一条单行道的桥。汽车可以从桥的西侧和东侧来。以下是我到目前为止的情况: 主要类别:Java 两线程共享数据的多线程编程,java,multithreading,Java,Multithreading,我应该制作一个程序,模拟汽车试图通过一条单行道的桥。汽车可以从桥的西侧和东侧来。以下是我到目前为止的情况: 主要类别: package project4.pkg2; import java.util.Scanner; public class Project42 { static int wbCars = 0; static int ebCars = 0; static boolean check = true; static void setCheck(int
package project4.pkg2;
import java.util.Scanner;
public class Project42 {
static int wbCars = 0;
static int ebCars = 0;
static boolean check = true;
static void setCheck(int i) {
if (i == 0){
check = false;
}
else
{
check = true;
}
}
static boolean check() {
return check;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Amount of west-bound cars: ");
wbCars = in.nextInt();
System.out.println("Amount of east-bound cars: ");
ebCars = in.nextInt();
WestBound myWest = new WestBound(wbCars);
Thread westThread = new Thread(myWest);
westThread.start();
EastBound myEast = new EastBound(ebCars);
Thread eastThread = new Thread(myEast);
eastThread.start();
}
}
package project4.pkg2;
import java.util.Scanner;
public class Project42 {
static int wbCars = 0;
static int ebCars = 0;
static boolean check = true;
static synchronized void setCheck(int i) {
if (i == 0){
check = false;
}
else
{
check = true;
}
}
static synchronized boolean check() {
return check;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Amount of west-bound cars: ");
wbCars = in.nextInt();
System.out.println("Amount of east-bound cars: ");
ebCars = in.nextInt();
WestBound myWest = new WestBound(wbCars);
Thread westThread = new Thread(myWest);
westThread.start();
EastBound myEast = new EastBound(ebCars);
Thread eastThread = new Thread(myEast);
eastThread.start();
}
}
东行班次:
package project4.pkg2;
import java.util.concurrent.TimeUnit;
class EastBound implements Runnable {
int cars = Project42.ebCars;
EastBound(int cars){
super();
this.cars = cars;
}
@Override
public void run() {
//System.out.println(cars + " <- cars");
while (cars > 0 && Project42.check() == true){
Project42.setCheck(0);
System.out.println("An east-bound car is driving on the bridge.");
try {
TimeUnit.SECONDS.sleep(3);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
Project42.setCheck(1);
System.out.println("The east-bound car has crossed the bridge.");
cars--;
}
}
}
package project4.pkg2;
import java.util.concurrent.TimeUnit;
public class WestBound implements Runnable {
int cars = Project42.wbCars;
WestBound(int cars){
super();
this.cars = cars;
}
@Override
public void run() {
//System.out.println("I made it this far!");
while (cars > 0 && Project42.check() == true){
Project42.setCheck(0);
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
System.out.println("A west-bound car is driving on the bridge.");
try {
TimeUnit.SECONDS.sleep(3);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
Project42.setCheck(1);
System.out.println("The west-bound car has crossed the bridge.");
cars--;
}
}
}
我的问题是在主课堂上,无论我先运行哪个线程(在本例中是我的westThread),只有我向西行驶的车辆通过,而没有一辆向东行驶的车辆通过。反之亦然,当我把东线程放在西线程之前
程序输出:
run:
Amount of west-bound cars:
2
Amount of east-bound cars:
3
A west-bound car is driving on the bridge.
The west-bound car has crossed the bridge.
A west-bound car is driving on the bridge.
The west-bound car has crossed the bridge.
BUILD SUCCESSFUL (total time: 9 seconds)
编辑:
谢谢你的帮助。对于有类似问题的未来观众,以下是我的解决方案:
主要类别:
package project4.pkg2;
import java.util.Scanner;
public class Project42 {
static int wbCars = 0;
static int ebCars = 0;
static boolean check = true;
static void setCheck(int i) {
if (i == 0){
check = false;
}
else
{
check = true;
}
}
static boolean check() {
return check;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Amount of west-bound cars: ");
wbCars = in.nextInt();
System.out.println("Amount of east-bound cars: ");
ebCars = in.nextInt();
WestBound myWest = new WestBound(wbCars);
Thread westThread = new Thread(myWest);
westThread.start();
EastBound myEast = new EastBound(ebCars);
Thread eastThread = new Thread(myEast);
eastThread.start();
}
}
package project4.pkg2;
import java.util.Scanner;
public class Project42 {
static int wbCars = 0;
static int ebCars = 0;
static boolean check = true;
static synchronized void setCheck(int i) {
if (i == 0){
check = false;
}
else
{
check = true;
}
}
static synchronized boolean check() {
return check;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Amount of west-bound cars: ");
wbCars = in.nextInt();
System.out.println("Amount of east-bound cars: ");
ebCars = in.nextInt();
WestBound myWest = new WestBound(wbCars);
Thread westThread = new Thread(myWest);
westThread.start();
EastBound myEast = new EastBound(ebCars);
Thread eastThread = new Thread(myEast);
eastThread.start();
}
}
西行:
package project4.pkg2;
import java.util.concurrent.TimeUnit;
public class WestBound implements Runnable {
int cars = Project42.wbCars;
WestBound(int cars){
super();
this.cars = cars;
}
@Override
public void run() {
//System.out.println("I made it this far!");
while (cars > 0){
if (Project42.check() == true){
Project42.setCheck(0);
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
System.out.println("A west-bound car is driving on the bridge.");
try {
TimeUnit.SECONDS.sleep(3);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
System.out.println("The west-bound car has crossed the bridge. West-bound cars remaining: "+(cars-1));
cars--;
Project42.setCheck(1);
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
}
}
System.out.println("All westbound cars crossed the bridge!");
}
}
东行:
package project4.pkg2;
import java.util.concurrent.TimeUnit;
class EastBound implements Runnable {
int cars = Project42.ebCars;
EastBound(int cars){
super();
this.cars = cars;
}
@Override
public void run() {
//System.out.println(cars + " <- cars");
while (cars > 0){
if (Project42.check() == true){
Project42.setCheck(0);
System.out.println("An east-bound car is driving on the bridge.");
try {
TimeUnit.SECONDS.sleep(3);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
System.out.println("The east-bound car has crossed the bridge. East-bound cars remaining: "+(cars-1));
cars--;
Project42.setCheck(1);
try {
TimeUnit.SECONDS.sleep(1);
}
catch (Exception e) {
System.out.println("Sumthin weird happened here.");
}
}
}
System.out.println("All eastbound cars crossed the bridge!");
}
}
packageproject4.pkg2;
导入java.util.concurrent.TimeUnit;
类东行实现可运行{
int cars=项目42.ebCars;
东行(国际车){
超级();
这个。汽车=汽车;
}
@凌驾
公开募捐{
//System.out.println(cars+“您可以学习Java线程同步的概念。它可以帮助您使用多个线程共享同一资源。
如果两个线程正在访问同一资源(例如,读取/写入check
变量),则必须同步对该资源的访问
在这种情况下,您必须将check()
和getCheck()
标记为synchronized
以及同步,在运行方法中需要另一个while循环。运行方法可以在汽车仍然非零时到达终点。使check
易失性(或使用AtomicBoolean
)。顺便问一下……为什么东行和西行需要两个类定义?一个类不能有两个实例吗?@BasilBourque我的任务规定“一个线程应该模拟西行车辆,另一个线程应该模拟东行车辆。”“@DontJudgeMe但如果行为相同,则不需要实现一对类。一个类、两个实例、两个线程。新流量(“西行”)
&新流量(“东行”)
没有理由说每个线程都需要有自己的类。每个线程都需要有自己的可运行的实例,但它们都可以是同一个类的实例。就像@BasilBourque所说的,您可能需要某种方式(例如一个参数或多个参数传递给构造函数)告诉每条线它的车应该朝哪个方向走。添加答案,而不是链接