Basic Java Synchronized Streams

I am learning Java but have synchronization issues. I want to print a list of numbers from many Java streams and each stream goes fine. I am getting an issue when using sync because I don't really understand. Can help you understand?

I need the output to see this, but sometimes the streams are in the wrong order. i want:

1-thread1
2-thread2
3-thread1
4-thread2
5-thread1
6-thread2
...
48-thread2
49-thread1

      

My broken codes:

public class ManyThreadsAdd {
    public static int index = 0;

    public static void main(String[] args) {
        ManyThreadsAdd myClass = new ManyThreadsAdd();
        Thread thread1 = new Thread(myClass.new RunnableClass());
        Thread thread2 = new Thread(myClass.new RunnableClass());

        thread1.start();
        thread2.start();
    }

    class RunnableClass implements Runnable {
        public synchronized void run() {
            while (index < 49) {
                try {
                    Thread.sleep(100);
                    System.out.println(index+"-" +Thread.currentThread());
                    index = index + 1;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

      

+3


source to share


2 answers


It depends on what you want to do.

An easy way to alternate the print order is to synchronize on the same object, in which case you can use an index or any other object.



public class ManyThreadsAdd {
    public static AtomicInteger index = new AtomicInteger(0);

    public static void main(String[] args) {
        ManyThreadsAdd myClass = new ManyThreadsAdd();
        Thread thread1 = new Thread(myClass.new RunnableClass());
        Thread thread2 = new Thread(myClass.new RunnableClass());

        thread1.start();
        thread2.start();
    }

    class RunnableClass implements Runnable {
        public void run(){
            synchronized(index){
                while(index.get() < 49){
                    try {
                      Thread.sleep(100);
                      System.out.println(index.get()+"-" +Thread.currentThread());
                      index.incrementAndGet();
                      index.notify();
                      index.wait();
                    } catch (InterruptedException e) {
                      e.printStackTrace();
                    }
                }
            }
        }
    }
}

      

+2


source


First, multithreading is inherently asynchronous; you cannot specify the order in which these threads are executed. If you need output like below, use a loop:

1-thread1
2-thread2
3-thread1
4-thread2
5-thread1
6-thread2
...
48-thread2
49-thread1

      



Second, you get nothing by adding the keyword synchronized

to public synchronized void run()

. This means that only one thread at a time can call this method at a time. Since you are creating new classes for each thread, this is pointless.

Third, if you need synchronization between your threads, use the queue to which you add tasks and which threads your reads one at a time.

+2


source







All Articles