java 如何同时(或在关闭时间)启动两个线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4676244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to start two threads at the same time(or at the close time)
提问by echo
I have a class Note
and a class Meeting
. There is an ArrayList
named noteList
in class Note
. When an object of Meeting
is created it is then registered in the noteList
.
我有一个班级Note
和一个班级Meeting
。有一种ArrayList
名为noteList
类Note
。当一个对象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). My program is:
我只想在主类中演示Meeting
可以同时(或在关闭时间)创建两个对象。我的程序是:
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. I have no synchronized methods in my program.
我看过的任何地方wait()
,notify()
或者notifyAll()
可以使用,但它们必须在synchronized方法中使用。我的程序中没有同步方法。
回答by Thilo
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 CountDownLatchon 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. You have no guarantee however, how concurrent the code after the latch will actually be executed.
这样做是消除了创建和启动线程的开销(在执行 run 方法之前发生的部分),并且可能还有一些奇怪的调度异常。但是,您无法保证闩锁之后的代码实际执行的并发程度。
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
}
回答by usr-local-ΕΨΗΕΛΩΝ
Unfortunately, there is no way to start two threads at the same time.
不幸的是,没有办法启动两个线程在同一时间。
Let me explain better: first of all, the sequence t1.Start();
and t2.Start();
is executed with t1 first and, after that, t2. It means onlythat thread t1 is scheduled beforethread 2, not actually started. The two methods take fractions of second each one, so the fact that they are in sequence cannot be seen by a human observer.
让我更好地解释一下:首先,序列t1.Start();
和t2.Start();
首先用 t1 执行,然后是 t2。这意味着只有线程 t1在线程 2之前被调度,而不是实际启动。这两种方法都需要几分之一秒的时间,因此人类观察者无法看到它们按顺序排列的事实。
More, Java threads are scheduled, ie. assigned to be eventuallyexecuted. 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.
更多,Java线程被调度,即。分配到最终执行。即使您有一个多核 CPU,您也不确定 1) 线程并行运行(其他系统进程可能会干扰)和 2) 线程都在Start()
调用方法后立即启动。
回答by epalm
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()
. Something like this:
您可以通过run()
在Meeting
类的方法顶部添加一条打印语句,并在t2.start()
. 像这样的东西:
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