简单的线程同步问题

时间:2013-02-24 05:47:53

标签: java multithreading synchronization

在下面的代码中,如果我同步bar方法,它在输出方面没有区别 - 我做错了什么?我相信同步“bar”将使“ThreaOne”打印10次,然后只打印“ThreadTwo”,但事实并非如此。我得到的输出如下:


I am Test thread:Thread OneOneOneOne
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread OneOneOneOne
I am in main now
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread Two
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread OneOneOneOne
I am Test thread:Thread Two
I am Test thread:Thread Two
I am Test thread:Thread Two
I am Test thread:Thread Two
I am in main now

等等。这是我的代码:

package com.rahul;

class ThreadTest implements Runnable{
    @Override
    public void run() {
        bar();
    }
    public synchronized void bar() {
        for(int i=0;i<10;i++){
            System.out.println("I am Test thread:"+Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
}

public class Test{
    public static void main(String[] args) {
        Thread t1 = new Thread(new ThreadTest(),"Thread OneOneOneOne");
        Thread t2 = new Thread(new ThreadTest(),"Thread Two");
        t1.start();
        try{
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        t2.start();
        while(true){
            System.out.println("I am in main now");
            try {
                t2.join();
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:3)

您有两个ThreadTest的实例。他们彼此无关;你没有打电话给同一个bar()

如果你这样做:

ThreadTest tt = new ThreadTest();
Thread t1 = new Thread(tt,"Thread OneOneOneOne");
Thread t2 = new Thread(tt,"Thread Two");

然后两个线程共享一个ThreadTest的实例,并且只有一个线程能够在该单个实例上一次调用bar()

答案 1 :(得分:2)

将方法声明为synchronized将不会产生预期效果。每个线程在其自己的ThreadTest实例上进行同步,因此调用不会进行交互。您需要在共享对象上同步一个线程以阻止另一个线程。例如:

class ThreadTest implements Runnable{
    private static Object LOCK_OBJECT = new Object();
    @Override
    public void run() {
        bar();
    }
    public void bar() {
        synchronized (LOCK_OBJECT) {
            for(int i=0;i<10;i++){
                System.out.println("I am Test thread:"+Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
}

答案 2 :(得分:0)

您正在同步的方法只能由一个线程访问,因为它们是实例方法,所以每个线程都调用它的自己的 bar方法。

也许你想将bar方法声明为static