一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Java多线程的同步问题

时间:2010-03-29 编辑:简简单单 来源:一聚教程网

这里我们在run()方法中加入了synchronized关键字,希望能对run方法进行互斥访问,但结果并不如我们希望那样,这是因为这里synchronized锁住的是this对象,即当前运行线程对象本身。代码中创建了10个线程,而每个线程都持有this对象的对象锁,这不能实现线程的同步。

    代码
    package com.vista;
    class MyThread implements java.lang.Runnable {
     private int threadId;

     public MyThread(int id) {
      this.threadId = id;
     }

     @Override
     public synchronized void run() {
      for (int i = 0; i < 100; ++i) {
       System.out.println("Thread ID: " + this.threadId + " : " + i);
      }
     }
    }

    public class ThreadDemo {
     /**
      * @param args
      * @throws InterruptedException
      */
     public static void main(String[] args) throws InterruptedException {
      for (int i = 0; i < 10; ++i) {
       new Thread(new MyThread(i)).start();
       Thread.sleep(1);
      }
     }
    }

    从上述代码段可以得知,要想实现线程的同步,则这些线程必须去竞争一个唯一的共享的对象锁。

    基于这种思想,我们将第一段代码修改如下所示,在创建启动线程之前,先创建一个线程之间竞争使用的Object对象,然后将这个Object对象的引用传递给每一个线程对象的lock成员变量。这样一来,每个线程的lock成员都指向同一个Object对象。我们在run方法中,对lock对象使用synchronzied块进行局部封锁,这样就可以让线程去竞争这个唯一的共享的对象锁,从而实现同步。

    代码
    package com.vista;

    class MyThread implements java.lang.Runnable {
     private int threadId;
     private Object lock;

     public MyThread(int id, Object obj) {
      this.threadId = id;
      this.lock = obj;
     }

     @Override
     public void run() {
      synchronized (lock) {
       for (int i = 0; i < 100; ++i) {
;       System.out.println("Thread ID: " + this.threadId + " : " + i);
       }
      }
     }
    }

 

    public class ThreadDemo {
     /**
      * @param args
      * @throws InterruptedException
      */
     public static void main(String[] args) throws InterruptedException {
      Object obj = new Object();
      for (int i = 0; i < 10; ++i) {
       new Thread(new MyThread(i, obj)).start();
       Thread.sleep(1);
      }
     }
    }

    从第二段代码可知,同步的关键是多个线程对象竞争同一个共享资源即可,上面的代码中是通过外部创建共享资源,然后传递到线程中来实现。我们也可以利用类成员变量被所有类的实例所共享这一特性,因此可以将lock用静态成员对象来实现,代码如下所示:

    代码
    package com.vista;

    class MyThread implements java.lang.Runnable {
     private int threadId;
     private static Object lock = new Object();

     public MyThread(int id) {
      this.threadId = id;
     }

     @Override
     public void run() {
      synchronized (lock) {
       for (int i = 0; i < 100; ++i) {
        System.out.println("Thread ID: " + this.threadId + " : " + i);
       }
      }
     }
    }

    public class ThreadDemo {
     /**
      * @param args
      * @throws InterruptedException
      */
     public static void main(String[] args) throws InterruptedException {
      for (int i = 0; i < 10; ++i) {
       new Thread(new MyThread(i)).start();
       Thread.sleep(1);
      }
     }
    }

    再来看第一段代码,实例方法中加入sychronized关键字封锁的是this对象本身,而在静态方法中加入sychronized关键字封锁的就是类本身。静态方法是所有类实例对象所共享的,因此线程对象在访问此静态方法时是互斥访问的,从而可以实现线程的同步,代码如下所示:

    代码
    package com.vista;

    class MyThread implements java.lang.Runnable{
     private int threadId;

 

     public MyThread(int id) {
      this.threadId = id;
     }

     @Override
     public void run() {
      taskHandler(this.threadId);
     }

     private static synchronized void taskHandler(int threadId) {
      for (int i = 0; i < 100; ++i) {
       System.out.println("Thread ID: " + threadId + " : " + i);
      }
     }
    }

    public class ThreadDemo {
     /**
      * @param args
      * @throws InterruptedException
      */
     public static void main(String[] args) throws InterruptedException {
      for (int i = 0; i < 10; ++i) {
       new Thread(new MyThread(i)).start();
       Thread.sleep(1);
      }
     }
    }

热门栏目