我的java代码没有同步

时间:2014-03-16 07:39:32

标签: java multithreading synchronization

class MultiplyTable1 {
    synchronized void printTable(int n) {
        for (int i = 1; i <= 5; i++) {
            System.out.println(n * i);
            try {
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }
}

class ThreadOne1 extends Thread {
    MultiplyTable1 mtObj = new MultiplyTable1();

    public void run() {
        mtObj.printTable(2);
    }
}

class ThreadTwo2 extends Thread {
    MultiplyTable1 mtObj = new MultiplyTable1();

    public void run() {
        mtObj.printTable(100);
    }
}

public class ThreadDemoDupe {

    public static void main(String[] args) {
        ThreadOne1 t1 = new ThreadOne1();
        ThreadTwo2 t2 = new ThreadTwo2();
        t1.start();
        t2.start();
    }
}

输出:

100
2
200
4
300
6
8
400
10
500

我的代码是什么,以获得输出:

2
4
6
8
10
100
200
300
400
500

100
200
300
400
500
2
4
6
8
10

我没有更多细节可以提供。

1 个答案:

答案 0 :(得分:1)

您正在创建两个单独的MultiplyTable1个对象。 synchronized实例方法有效地使用:

synchronized (this) {
    ...
}

因此,如果您在两个不同的对象上调用该方法,它们仍然可以并行运行。要查看具有效果的同步,您需要在公共对象上进行同步。您可以通过将其更改为不在MultiplyTable1对象本身上进行同步来实现,或者您可以只为两个线程提供相同的MultiplyTable1对象。例如:

class MultiplyTable1 {
    synchronized void printTable(int n) {
        for (int i = 1; i <= 5; i++) {
            System.out.println(n*i);
            try {
                Thread.sleep(500);
            }
            catch (InterruptedException e) {
                System.out.println(e);
            }
        }
    }
}

// Prefer implementing Runnable over extending Thread.
// In reality I'd only have a single class and parameterize
// the value passed to the printTable method, but...
class Runnable1 implements Runnable {
    private final MultiplyTable1 table;

    Runnable1(MultiplyTable1 table) {
        this.table = table;
    }

    @Override public void run() {
        table.printTable(2);
    }
}

class Runnable2 implements Runnable {
    private final MultiplyTable1 table;

    Runnable2(MultiplyTable1 table) {
        this.table = table;
    }

    @Override public void run() {
        table.printTable(100);
    }
}

public class ThreadDemoDupe {
    public static void main(String[] args) {
        MultiplyTable1 table = new MultiplyTable1();
        Thread t1 = new Thread(new Runnable1(table));
        Thread t2 = new Thread(new Runnable2(table));
        t1.start();
        t2.start();
    }
}