Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Thread sequential order

Following code found that output results is not the order, not from small to large, how to guarantee it is order from small to large?

java code

public class TestSync {  

    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        for (int i = 0; i < 10; i++) {  
            new Thread(new Thread1()).start();  
        }  

    }  

    public int getNum(int i) {  
        synchronized (this) {  
            i++;  
        }  
        return i;  
    }  

    static class Thread1 implements Runnable {  
        static Integer value = 0;  
        @Override  
        public void run() {  
            TestSync ts = new TestSync();  
            value = ts.getNum(value);  
            System.out.println("thread1:" + value);  
        }  
    }  

}  
like image 558
suannai Avatar asked Jan 29 '26 15:01

suannai


2 Answers

What are you trying to accomplish? Your code is only synchronizing calls made to a particular TestSync instance. Since each thread creates its own instance, it's like you are not synchronizing anything at all. Your code is not doing anything to synchronize or coordinate accesses across the different threads.

I'd suggest the following code may be more along the lines of what you are trying to accomplish:

public static void main (String[] args) throws java.lang.Exception {
        for (int i = 0; i < 10; i++) {  
            new Thread1().start();  
        }  
}

//no need for this to be an instance method, or internally synchronized
public static int getNum(int i) {  
       return i + 1;  
}

static class Thread1 extends Thread {  
    static Integer value = 0;  

    @Override  
    public void run() {  
        while (value < 100) {
            synchronized(Thread1.class) {  //this ensures that all the threads use the same lock
                value = getNum(value);  
                System.out.println("Thread-" + this.getId() + ":  " + value);  
            }

            //for the sake of illustration, we sleep to ensure some other thread goes next
            try {Thread.sleep(100);} catch (Exception ignored) {} 
        }
    }  
}

Live example here: http://ideone.com/BGUYY

Note that getNum() is essentially superfluous. The example above would work the same if you replaced value = getNum(value); with a simple value++;.

like image 198
aroth Avatar answered Feb 01 '26 04:02

aroth


Though one wonders why this is necessary, here is one way to do it. It is not elegant but represents a minimal change to the original program:

import java.util.concurrent.*;

public class TestSync {

    public static void main(String[] args) {

    ExecutorService service = Executors.newSingleThreadExecutor();
    for (int i = 0; i < 10; i++) {
        service.submit(new Thread1());
    }

}

public int getNum(int i) {
    synchronized (this) {
        i++;
    }
    return i;
}

static class Thread1 implements Runnable {
    static Integer value = 0;
    @Override
    public void run() {
        TestSync ts = new TestSync();
        value = ts.getNum(value);
        System.out.println("thread1:" + value);
    }
}

}

Here is a version that is better. It uses an AtomicInteger for the counter (which is probably overkill in this case) to remove the unpleasant getNum() method:

import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

public class TestSync {  
    static private AtomicInteger i = new AtomicInteger(0);

    public static void main(String[] args) {  
        ExecutorService service = Executors.newSingleThreadExecutor();
        for (int i = 0; i < 10; i++) {  
            service.submit(new MyThread(i));  
        }  
        try { Thread.sleep(2*1000); } catch(Exception ex) {}
        service.shutdown();
    }  

    static class MyThread implements Runnable {  
        private int num = 0;
        public MyThread(int num) {
            this.num = num;
        }
        @Override  
        public void run() {  
            int value = i.incrementAndGet();
            System.out.println("thread # " + num + " value = " + value);  
        }  
    }  
}  
like image 21
Michael Easter Avatar answered Feb 01 '26 05:02

Michael Easter