简体   繁体   English

如何同时(或在关闭时)启动两个线程

[英]how to start two threads at the same time(or at the close time)

I have a class Note and a class Meeting . 我有课堂Note和课堂Meeting There is an ArrayList named noteList in class Note . Note有一个名为noteListArrayList When an object of Meeting is created it is then registered in the noteList . 创建Meeting对象后,它将在noteList注册。

I just want to demostrate in the main class that two objects of Meeting can be created at the same time (or at the close time). 我只想在主类中说明可以同时(或在关闭时)创建Meeting两个对象。 My program is: 我的计划是:

public class Note{
    //some field and method hier
    public void add(Meeting m){
        notes.add(m);
    }
    private  static final List<Entry> notes =
        Collections.synchronizedList(new ArrayList<Entry>());
}

public class Meeting implements Runnable{
    public Meeting(Note note_1,Note note_2,Calendar calendar){
        note_1.add(this);
        note_2.add(this);}
        //some method
    }

    public class Test implements Runnable{
        public static void main(String[] args) {
            Note note_1 = new Note();                
            Note note_2 = new Note();
            Meeting m_1 = new Meeting(note_1,note_2);
            Meeting m_2 = new Meeting(note_2,note_1)
            Thread t1 = new Thread(m_1);
            Thread t2 = new Thread(m_2)
            t1.start();
            t2.start();
        }
        //t1,t2 are two thread and they start one to one(not at the same time).

I have read anywhere that wait() , notify() or notifyAll() can be used, but they must be used in a synchronized method. 我已经读过可以使用wait()notify()notifyAll()任何地方,但它们必须在同步方法中使用。 I have no synchronized methods in my program. 我的程序中没有同步方法。

This is as close as you are going to get to starting the two threads. 这就像你要开始两个线程一样接近。

What you could do to synchronize the run methods even more is to have them wait on a CountDownLatch on the top of their run methods. 你可以做的更多同步运行方法是让他们在运行方法的顶部等待CountDownLatch

What this does is taking away the overhead of creating and starting Threads (the part that happens before your run method gets executed), and maybe also some freak scheduling oddities. 这样做是为了消除创建和启动Threads(在运行方法执行之前发生的部分)的开销,也可能是一些怪异的调度奇怪。 You have no guarantee however, how concurrent the code after the latch will actually be executed. 但是,您无法保证实际执行锁存器后代码的并发性。

CountDownLatch latch = new CountDownLatch(2);

Runnable r1 = new Meeting(latch);
Runnable r2 = new Meeting(latch);


// in Meeting

private final CountDownLatch latch;

public void run(){

   latch.countDown();
   latch.await();

   // other code
}

Unfortunately, there is no way to start two threads at the same time . 不幸的是,有没有办法在同一时间启动两个线程。

Let me explain better: first of all, the sequence t1.Start(); 让我解释一下:首先,序列为t1.Start(); and t2.Start(); t2.Start(); is executed with t1 first and, after that, t2. 首先用t1执行,然后执行t2。 It means only that thread t1 is scheduled before thread 2, not actually started. 这意味着只有线程t1 线程2 之前被调度,而不是实际启动。 The two methods take fractions of second each one, so the fact that they are in sequence cannot be seen by a human observer. 这两种方法各占一小部分,因此人类观察者无法看到它们按顺序排列的事实。

More, Java threads are scheduled , ie. 更多,Java线程被安排 ,即。 assigned to be eventually executed. 被指派最终被执行。 Even if you have a multi-core CPU, you are not sure that 1) the threads run in parallel (other system processes may interfere) and 2) the threads both start just after the Start() method is called. 即使你有一个多核CPU,你也不确定1)线程并行运行(其他系统进程可能会干扰)和2)线程都在调用Start()方法之后Start()

They are starting at "close" to the same time. 他们开始时间“接近”。 The point is that your code isn't blocking at t1.start() . 关键是你的代码没有在t1.start()处阻塞。

You might be able to see this by adding a print statement at the top of the run() method of the Meeting class, and another print statement right after t2.start() . 您可以通过在Meeting类的run()方法的顶部添加print语句,并在t2.start()之后t2.start()另一个print语句来查看此内容。 Something like this: 像这样的东西:

public class Meeting implements Runnable {
    private String name;
    public Meeting(String name) {
        this.name = name;
    }
    public void run() {
        System.out.println(name + " is running");
    }
}

public class Test {
    public static void main(String[] args) {
        Meeting m_1 = new Meeting("meeting 1");
        Meeting m_2 = new Meeting("meeting 2")
        Thread t1 = new Thread(m_1);
        Thread t2 = new Thread(m_2)
        t1.start();
        t2.start();
        System.out.println("this might print first!");
    }
}

// possible output:
> this might print first!
> meeting 1 is running
> meeting 2 is running

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

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