简体   繁体   中英

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

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 .

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:

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.

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.

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.

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(); and t2.Start(); is executed with t1 first and, after that, t2. It means only that thread t1 is scheduled before thread 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.

More, Java threads are scheduled , ie. 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.

They are starting at "close" to the same time. The point is that your code isn't blocking at 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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