Chinaunix首页 | 论坛 | 博客
  • 博客访问: 145536
  • 博文数量: 54
  • 博客积分: 2682
  • 博客等级: 少校
  • 技术积分: 580
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 20:56
文章分类
文章存档

2012年(2)

2011年(10)

2010年(28)

2009年(14)

我的朋友

分类: Java

2009-11-24 16:05:09

java的同步机制,大概是通过
1.synchronized;
2.Object方法中的wait,notify;
3.ThreadLocal机制
来实现的
其中synchronized有两种用法
1.对类的方法进行修饰
2.synchronized(对象)的方法进行修饰
 
所以我们这里有好几种场景,现在我对每个场景都举个例子说明,并且指出哪个例子是说明哪个场景的。最后说为什么需要ThreadLocal,以及ThreadLocal的用法。例子结合了从网上找的资料。
 
所有场景都要用到的类
 
 

package threadlocal.test;

public class Student {
    private int age = 0;

    public int getAge() {
        return this.age;

    }

    public void setAge(int age) {
        this.age = age;
    }
}

 

一,第一个应用场景,无任何同步机制,整个程序运行时间约为5秒


 

package threadlocal.test;

import java.util.Random;

public class ThreadDemo1 implements Runnable {
    Student student = new Student();

    public static void main(String[] agrs) {
        ThreadDemo1 td = new ThreadDemo1();
        Thread t1 = new Thread(td, "a");
        Thread t2 = new Thread(td, "b");
        t1.start();
        t2.start();

    }

    public void run() {
        accessStudent();
    }

    public void accessStudent() {
        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running!");
        Random random = new Random();
        int age = random.nextInt(100);
        System.out
                .println("thread " + currentThreadName + " set age to:" + age);

        this.student.setAge(age);
        System.out.println("thread " + currentThreadName
                + " first read age is:" + this.student.getAge());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println("thread " + currentThreadName
                + " second read age is:" + this.student.getAge());

    }

}


简述:两个线程同时开启,对同一个类实例进行操作,虽然速度很快,只占用一个线程运行时间,但是出现竞争使得整个代码变得无用。

 

二,第二个应用场景,synchronized修饰类方法。程序运行时间约为10秒

 

package threadlocal.test;

import java.util.Random;

/**
 * 加了个synchronized,不会出现竞争了,但是效率极低
 *
 * @author dachuan
 *
 */

public class ThreadDemo2 implements Runnable {
    Student student = new Student();

    public static void main(String[] agrs) {
        ThreadDemo2 td = new ThreadDemo2();

        // ThreadDemo2 td = new ThreadDemo2();

        // ThreadDemo2 td2 = new ThreadDemo2();


        Thread t1 = new Thread(td, "a");
        Thread t2 = new Thread(td, "b");
        //之所以t1,t2两个线程会在acessStudent函数处出现同步,是因为他们都是对同一个实例的函数进行访问

        //t1,t2都在对ThreadDemo2这个类的实例td的函数accessStudent进行访问,所以会出现同步。

        //也就是说,如果类A内部定义了一个synchronized方法,而a是A的一个实例,如果有不同的线程都对a.syn()进行访问,就会出现同步

        //synchronized关键字是锁住了类的实例,让另外的线程进不去

        //在这个例子中,t1,t2两个线程,总共运行时间肯定>=10秒,因为t1,t2在accessStudent这个地方是串行的。

        //如果t1,t2是为了完成某件事情在合作,那么这个同步是值得的。

        //现在的前提是,t1,t2必须使用同一个类的实例,但是又做的都是独立的事情。这个时候,synchronized方法显得很低效,反而降低了运算效率

        //所以ThreadLocal的出现就是为了解决这个问题的。

        //ThreadLocal的应用场合:

        //1.多个线程都要使用同一个类实例

        //2.这多个线程拿到这个类实例后做的事情是互相独立的

        //生产者和消费者模型跟ThreadLocal的应用场合有一个区别

        //1.生产者和消费者两个线程其实也是在使用同一个类实例

        //2.但是这两个线程拿到类实例后做的事情是互相牵制的

        t1.start();
        t2.start();

    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */

    public void run() {
        accessStudent();
    }

    public synchronized void accessStudent() {
        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running!");
        // System.out.println("first read age is:"+this.student.getAge());

        Random random = new Random();
        int age = random.nextInt(100);
        System.out
                .println("thread " + currentThreadName + " set age to:" + age);

        this.student.setAge(age);
        System.out.println("thread " + currentThreadName
                + " first read age is:" + this.student.getAge());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println("thread " + currentThreadName
                + " second read age is:" + this.student.getAge());

    }

}


简述:两个线程在accessStudent方法处串行操作,所以整个程序时间变长,大约10秒。消除了竞争

 

三,第三个应用场景,synchronized修饰类,程序运行时间大约10秒。

 

package threadlocal.test;

import java.util.Random;

/**
 *
 * @author dachuan
 *
 */

public class ThreadDemo3 implements Runnable {
    Student student = new Student();

    public static void main(String[] agrs) {
        ThreadDemo3 td = new ThreadDemo3();
        Thread t1 = new Thread(td, "a");
        Thread t2 = new Thread(td, "b");
        t1.start();
        t2.start();

    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */

    public void run() {
        accessStudent();
    }

    public void accessStudent() {
        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running!");
        // System.out.println("first read age is:"+this.student.getAge());

        synchronized (this) {

            Random random = new Random();
            int age = random.nextInt(100);
            System.out.println("thread " + currentThreadName + " set age to:"
                    + age);

            this.student.setAge(age);
            System.out.println("thread " + currentThreadName
                    + " first read age is:" + this.student.getAge());
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println("thread " + currentThreadName
                + " second read age is:" + this.student.getAge());

    }

}

 

简述:synchronized关键字都是用来锁住对象的,这个地方锁住了this,两个线程访问的都是同一个类实例,所以两个线程中的this都是指一个东西。整个程序无竞争,运行时间大约10秒

 

四,第四个应用场景,TheadLocal。程序无竞争,运行时间大约5秒


 

package threadlocal.test;

import java.util.Random;

public class ThreadLocalDemo implements Runnable {
    private final static ThreadLocal studentLocal = new ThreadLocal();

    public static void main(String[] agrs) {
        ThreadLocalDemo td = new ThreadLocalDemo();
        Thread t1 = new Thread(td, "a");
        Thread t2 = new Thread(td, "b");

        t1.start();
        t2.start();

    }

    /*
     * (non-Javadoc)
     *
     * @see java.lang.Runnable#run()
     */

    public void run() {
        accessStudent();
    }

    public void accessStudent() {

        String currentThreadName = Thread.currentThread().getName();
        System.out.println(currentThreadName + " is running!");
        Random random = new Random();
        int age = random.nextInt(100);
        System.out
                .println("thread " + currentThreadName + " set age to:" + age);
        Student student = getStudent();
        student.setAge(age);
        System.out.println("thread " + currentThreadName
                + " first read age is:" + student.getAge());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println("thread " + currentThreadName
                + " second read age is:" + student.getAge());

    }

    protected Student getStudent() {
        Student student = (Student) studentLocal.get();
        if (student == null) {
            student = new Student();
            studentLocal.set(student);
        }
        return student;
    }

    protected void setStudent(Student student) {
        studentLocal.set(student);
    }
}


简述:ThreadLocal的出现使得两个线程虽然手中握有同一个类的实例,但是做同一样事情的时候却互不干扰。这就是ThreadLocal的应用场景。

 

五,第五个应用场景,生产者&消费者模型和ThreadLocal模型的区别

我个人总结,ThreadLocal的应用场景为:

1.多个线程都要使用同一个类实例
2.这多个线程拿到这个类实例后做的事情是互相独立的

 

而生产者消费者例子跟ThreadLocal有一点区别

1.生产者和消费者两个线程其实也是在使用同一个类实例
2.但是这两个线程拿到类实例后做的事情是互相牵制的

 

所以看到这里,就知道ThreadLocal的应用场景是什么了。

 

我在阅读Hadoop源码时,看到MapOutputFile这个类中使用了ThreadLocal,所以查了些资料总结了下。

 

在Hadoop源码中,有一处地方对Object的wait和notify进行了很好的诠释。以后有空再记录一下。

阅读(588) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~