在另一个线程完成其操作后,如何运行线程

时间:2015-11-02 23:48:37

标签: java multithreading

我正在尝试模拟一个停车场入口系统,该系统包含2个楼层,20个房间,2个入口和2个出口。我在Java中使用Threads,并且我尝试使用idthread.wait(),但是在另一个线程结束后,它们没有帮助使线程运行。

这是我到目前为止所提出的:

ShardedDataThread.java

thread.sleep

SharedData.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SharedDataThread extends Thread {
      private SharedData mySharedData;
      private String myThreadName;
      public int L1 = 20;
      public int L2 = 20;


//Setup the thread
 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
SharedDataThread(String name, SharedData sharedstuff) {
    super(name);
    mySharedData=sharedstuff;
    myThreadName=name;
}

//This is called when "start" is used in the calling method

public void run() {
     while(true){

    System.out.println("Entry or Exit?");
    String input = null;
    try {
        input = br.readLine();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }



    if (input.equalsIgnoreCase("entry"))  {

            try {
              // Acquire the lock using the acquireLock() method
              // The thread will pause here on wait() until it gets a lock
              mySharedData.acquireLock();

               //use park method to occupy one space
              this.park(); // Increment the number of reads
              mySharedData.releaseLock(); 
            }
            catch(InterruptedException e) {
              System.err.println("Failed to get lock when reading:"+e);
            }
          }
          else {

            try {

                mySharedData.acquireLock();
                System.out.println(myThreadName+" is writing");
                exit(); // INCREMENT ONE SPOT

                mySharedData.releaseLock(); // releases the lock
            }
            catch(InterruptedException e) {
              System.err.println("Failed to get lock when writing:"+e);
            }
         }

      System.out.println("L1 has : "+L1+" "+"L2 has: "+" "+L2);

    }
 //while ends
}
//State method


    public void park() {
        if (L1>0 && L1<=20){
            L1= L1-1;   
            }

            else if (L1==0 && L2<=20 && L2>0){
                L2= L2-1;   

                }
            else if (L1==0 && L2==0){
                 System.out.println("No Spaces Left");
                }   
}

    public void exit() {
         if (L1<20) {
             L1 = L1 +1;
         } else if (L2<20) {
             L2 = L2+1;
         }  
}

      public static void main(String[] args) throws InterruptedException  {

            SharedData mySharedData = new SharedData();



            SharedDataThread myThread1 = new SharedDataThread("Entry1", mySharedData);
            //SharedDataThread myThread2 = new SharedDataThread("Entry2", mySharedData);
            //SharedDataThread myThread3 = new SharedDataThread("Exit1", mySharedData);
            //SharedDataThread myThread4 = new SharedDataThread("Exit2", mySharedData);

            // Now start the threads executing

            myThread1.start();
            //myThread1.join();
            //myThread2.start();
            //myThread2.join();
            //myThread3.start();
            //myThread3.join();
            //myThread4.wait();
          }


}

2 个答案:

答案 0 :(得分:1)

这从来没有任何意义:

myThread1.start();
myThread1.join();

启动一个线程然后立即等待它完成永远没有任何意义。拥有线程的全部意义在于,不同的线程可以同时执行不同的事情

如果在myThread1运行时调用者不想执行任何其他操作,则没有理由创建myThread1。调用者应该做myThread1此时所做的任何事情。

不要使用线程来模拟模拟中的对象。使用线程执行工作。通常,这意味着,使用线程池(例如java.util.concurrent.ThreadPoolExecutor)来执行任务

如果您希望模拟实时运行,请不要使用sleep()的线程。使用计划程序(例如java.util.concurrent.ScheduledThreadPoolExecutor)执行延迟任务。

答案 1 :(得分:0)

你正在考虑这个问题。在线程完成后不要考虑等待线程做某事。考虑在完成一些工作后做某事。这意味着工作必须是可以等待的。