简体   繁体   English

Java Thread概念/演示

[英]Java Thread concept / demo

I'm trying to write a Java program that uses threads. 我正在尝试编写一个使用线程的Java程序。 I want to be able to run 3 threads when the program starts, and have them waiting on an ArrayList of "work orders". 我希望能够运行3个线程在程序启动时,并让他们等待的“工作单”的ArrayList。 Initially, theere will be no work orders. 最初,剧场将没有工作指令。 So the Threads should just wait. 因此线程应等待。 At some point in the future, work orders will be added to the ArrayList, and the main thread must notify the threads that there is work to do. 在将来的某个时候,工作指令将添加到ArrayList中,并且主线程必须通知线程有工作要做。

I want to be able to do it by extending Thread (instead of implementing Runnable). 我希望能够通过扩展Thread(而不是实现Runnable)来做到这一点。

I think the main issue I have is that the threads are not correctly synchronized with the workorders ArrayList. 我认为主要的问题我已经是线程都不能正确地与工作订单ArrayList的同步。

My code looks like this: 我的代码如下所示:

  public static void main(String[] args) {

    AnotherRunnable anotherRunnable = new AnotherRunnable();
    ArrayList<ATMRunnable> workOrders = new ArrayList<ATMRunnable>();

    T1 t1 = new T1(anotherRunnable, workOrders);
    T1 t2 = new T1(anotherRunnable, workOrders);

    t1.start();
    t2.start();

    try{
        Thread.sleep(2000);
    }
    catch(InterruptedException e){}

        synchronized (workOrders){

        System.out.println(t1.getState() + " - " + t1.getName());
        System.out.println(t2.getState() + " - " + t2.getName());

        System.out.println("notify");
        workOrders.notify();

        System.out.println(t1.getState() + " - " + t1.getName());
        System.out.println(t2.getState() + " - " + t2.getName());

    }
  }

The AnotherRunnable class: 该AnotherRunnable类:

public class AnotherRunnable implements Runnable {

public void run()
{
    System.out.println("AnotherRunnable");

}
}

And the Tread class: 和Tread类:

public class T1 extends Thread { 公共类T1扩展了线程{

 AnotherRunnable anotherRunnable;
ArrayList<ATMRunnable> workOrders;

ATMThread(AnotherRunnable anotherRunnable, ArrayList<ATMRunnable> workOrders)
{
    this.anotherRunnable = anotherRunnable;
    this.workOrders = workOrders;
}

public void run()
{
    System.out.println("Run Thread");

    synchronized (workOrders){
        try{
            System.out.println("wait Thread");
            workOrders.wait();
        }
        catch (InterruptedException e){}
    }

}

} }

This is the output of the program: 这是程序的输出:

Run Thread
wait Thread
Run Thread
wait Thread
WAITING - Thread-1
WAITING - Thread-2
notify all
BLOCKED - Thread-1
WAITING - Thread-2

As you can see, the state of the first thread is changed to Blocked, after the call to notify on the workOrders object. 如您所见,在对workOrders对象进行通知的调用之后,第一个线程的状态更改为“已阻止”。 But neither the threads nor the runnable object is executed. 但是线程和可运行对象均未执行。

Any help will be appreciated. 任何帮助将不胜感激。

You should use concurrent collections in java to avoid manual synchronization as far as possible. 您应该在Java中使用并发集合,以尽可能避免手动同步。

I want to be able to run 3 threads when the program starts, and have them waiting on an ArrayList of "work orders". 我希望能够运行3个线程在程序启动时,并让他们等待的“工作单”的ArrayList。 Initially, theere will be no work orders. 最初,剧场将没有工作指令。 So the Threads should just wait. 因此线程应等待。 At some point in the future, work orders will be added to the ArrayList, and the main thread must notify the threads that there is work to do. 在将来的某个时候,工作指令将添加到ArrayList中,并且主线程必须通知线程有工作要做。

For this kind of synchronization, Blocking queues are your friends like LinkedBlockingQueue which make threads wait when there's no item in the queue or when the queue is full. 对于这种同步, 阻塞队列是您喜欢的LinkedBlockingQueue之类的朋友,当队列中没有任何项目或队列已满时,它们使线程等待。 You do not need any synchronized/wait/notify there. 您不需要在那里进行任何同步/等待/通知。

You can also check if it helps: synchronization is risky 您还可以检查是否有帮助: 同步存在风险

If it is only for learning purpose, you have to make your synchronization proper first logically. 如果仅出于学习目的,则必须首先在逻辑上使同步正确。 It doesn't use any condition for waiting or notifying which is problematic. 它不使用任何条件等待或通知问题。 It'll work if it is proper but its not a preferred way. 如果合适,它将起作用,但不是首选方法。

I found what I had to do to get this working. 我发现我需要做些什么才能使它工作。 Mainly, it was the concept what was missing. 主要是缺少该概念。 I had to loop in the run method of the thread. 我不得不循环运行线程的方法。 I was thinking that that method would be called every time notifyall is invoked, which is not true. 我以为每次调用notifyall都会调用该方法,这是不正确的。 When wait() is call on the synchronized object, the thread is stopped, and with notify it resumes execution, if that code is not in a loop, it wont be executed. 当在同步对象上调用wait()时,线程将停止,并通过通知它恢复执行,如果该代码不在循环中,则将不执行该线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM