线程同步行为

时间:2013-03-22 10:38:49

标签: java multithreading synchronized

class Untitled {
    public static void main(String[] args) {
        MyRunnable r1 = new MyRunnable();
        Thread t1 = new Thread(r1,"Thread 1:");
        Thread t2 = new Thread(r1,"Thread 2:");
        t1.start();
        t2.start();

    }
}

class MyRunnable implements Runnable
{
    String s1 = "Hello World";
    String s2 = "Hello New World";
    public void run()
    {
        synchronized(s1)
        {
            for(int i =0;i<3;++i)
            System.out.println(Thread.currentThread().getName()+s1);

        }
        synchronized(s2)
        {
            for(int i = 0;i<3;++i)
            System.out.println(Thread.currentThread().getName()+s2);
        }

    }
}

输出:

Thread 1:Hello World
Thread 1:Hello World
Thread 1:Hello World
Thread 1:Hello New World
Thread 2:Hello World
Thread 1:Hello New World
Thread 2:Hello World
Thread 1:Hello New World
Thread 2:Hello World
Thread 2:Hello New World
Thread 2:Hello New World
Thread 2:Hello New World

为什么Thread2方法在run()执行第一个同步块时Thread1执行第二个同步块,即使锁定对象不同也是如此。Thread2在第一个同步的块中执行等待,直到Thread1离开该块?

如果是这样,如何使两个同步块同时运行??

7 个答案:

答案 0 :(得分:6)

  

Thread2的执行是否在第一个同步块中等待,直到Thread1离开该块??

是的,这是主意 - thread2一个接一个地执行块。如果它被阻止但无法进入第一个,它将在那里等待,直到s1锁定可用。

  

如果是这样,如何使两个同步块同时运行??

您需要将它们拆分为两个不同的runnable,并为每个runnable使用一个线程。

答案 1 :(得分:3)

因为在run方法中,语句是按顺序执行的,而不是并行执行。

所以谁有线程1或线程2获得s1的锁定其他人将等待,直到它被释放。

答案 2 :(得分:3)

这两个块是一个接一个的,这意味着线程2必须通过块1才能通过块2

答案 3 :(得分:3)

  

为什么Thread2不能在run()中执行第二个同步块   Thread1执行第一个同步块时的方法

代码逐行执行,执行不会跳转到下一个块,线程2等待线程1离开第一个同步块。

  

Thread2的执行是否在第一个同步块中等待,直到   Thread1离开那个块

是。

  

如果是这样,如何使两个同步块同时运行??

所以请将它们保存在单独的Runnable实例中。并且在序列中没有一个接一个。

答案 4 :(得分:1)

只是编译器执行代码,即编译器按照它们的编写顺序执行代码。线程2不能跳过第一个代码块。它将首先执行第一个块,然后执行其他块。

答案 5 :(得分:0)

以下是让它们同时运行的示例。请注意,我不仅将每个循环放在一个单独的线程中,而且还将同步范围缩小到只是打印。

public class Test {
  static class MyRunnable implements Runnable {
    final String s1 = " Hello World - ";
    final String s2 = " Hello New World - ";
    static final int n = 10;

    @Override
    public void run() {
      // Two loops in two threads.
      new Thread(new Runnable() {
        @Override
        public void run() {
          for (int i = 0; i < n; ++i) {
            // Scope reduced.
            synchronized (s1) {
              System.out.println(Thread.currentThread().getName() + s1 + i);
            }
          }
        }
      }, Thread.currentThread().getName() + "(a)").start();
      // Two loops in two threads.
      new Thread(new Runnable() {
        @Override
        public void run() {
          for (int i = 0; i < n; ++i) {
            // Scope reduced.
            synchronized (s2) {
              System.out.println(Thread.currentThread().getName() + s2 + i);
            }
          }
        }
      }, Thread.currentThread().getName() + "(b)").start();
    }
  }

  public void test() {
    MyRunnable r1 = new MyRunnable();
    Thread t1 = new Thread(r1, "Thread 1:");
    Thread t2 = new Thread(r1, "Thread 2:");
    t1.start();
    t2.start();
  }

  public static void main(String args[]) {
    new Test().test();
  }
}

答案 6 :(得分:0)

class ThreadExample {
    public static void main(String[] args) {
        MyRunnable15756 r1 = new MyRunnable15756();
        Thread t1 = new Thread(r1,"Thread 1:");
        Thread t2 = new Thread(r1,"Thread 2:");
        t1.start();
        t2.start();

    }
}

class MyRunnable15756 implements Runnable
{
    String s1 = "Hello World";
    String s2 = "Hello New World";
    Runnable runnable1 =  new Runnable(){
        @Override
        public void run() {
              synchronized(s1)
                {
                    for(int i =0;i<30;++i)
                    System.out.println(Thread.currentThread().getName()+s1);

                }

        }
    };

    Runnable runnable2 =  new Runnable(){
        @Override
        public void run() {
             synchronized(s2)
                {
                    for(int i = 0;i<30;++i)
                    System.out.println(Thread.currentThread().getName()+s2);
                }

        }
    };
    public void run()
    {

        new Thread(runnable1).start();

        new Thread(runnable2).start();



    }
}
相关问题