多线程编程,两个线程共享数据

时间:2017-10-21 14:24:54

标签: 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 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();
    }

}

EastBound课程:

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--;  
        }
    }
}

WestBound课程:

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),只有我的西方车辆交叉,而东方车辆都没有。当我把eastThread放在我的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 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();
    }

}

WestBound:

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!");
    }
}

3 个答案:

答案 0 :(得分:0)

您可以学习Java的概念 - 线程同步..它可以帮助您使用多个线程来共享相同的资源。 Try this link to learn more

答案 1 :(得分:0)

如果两个线程正在访问相同的资源(例如,读/写check变量),则必须同步对它的访问。

在这种情况下,您必须将check()getCheck()标记为synchronized

答案 2 :(得分:0)

除了同步之外,还需要在run方法中使用另一个while循环。当汽车仍然不为零时,运行方法可以到达终点。