代码之家  ›  专栏  ›  技术社区  ›  tez

线程同步行为

  •  1
  • tez  · 技术社区  · 11 年前
    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 正在执行第一个同步块,即使锁定对象不同。是否 螺纹2 的执行在第一个同步块处等待,直到 螺纹1 离开那个街区??

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

    7 回复  |  直到 11 年前
        1
  •  6
  •   assylias    11 年前

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

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

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

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

        2
  •  3
  •   Subhrajyoti Majumder MGPJ    11 年前

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

    因此,线程1或线程2中的任何一个都将等待s1的锁被释放。

        3
  •  3
  •   phoenix7360    11 年前

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

        4
  •  3
  •   Pheonix    11 年前

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

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

    Thread2的执行是否在第一个同步块处等待,直到 线程1离开该块

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

    因此,将它们保存在单独的可运行实例中。而不是一个序列中一个接一个。

        5
  •  1
  •   Abhishek Kumar    11 年前

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

        6
  •  0
  •   OldCurmudgeon    11 年前

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

    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();
      }
    }
    
        7
  •  0
  •   Abhishek Kumar    11 年前
    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();
    
    
    
        }
    }