Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
echo Avatar asked Jan 13 '11 02:01

echo


2 Answers

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
}
like image 76
Thilo Avatar answered Oct 04 '22 00:10

Thilo


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.

like image 42
usr-local-ΕΨΗΕΛΩΝ Avatar answered Oct 03 '22 23:10

usr-local-ΕΨΗΕΛΩΝ