java中synchronized的范例

来源:互联网 发布:java 字符串乱码 编辑:程序博客网 时间:2024/05/21 06:18
java中Synchronized的范例:


(1)同步的关键:
同步的关键是多个线程对象竞争同一个共享资源即可。要想实现线程的同步,则多个线程必须去竞争一个唯一的共享的对象锁。


(二)描述:
创建10个线程,每个线程都打印从0到99这100个数字,我们希望线程之间不会出现交叉乱序打印,而是顺序地打印。


(1)方法一:


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);
        }
    }
}


分析:不可以顺序打印;


(2)方法二:


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);
        }
    }
}


分析:通过外部创建共享资源,然后传递到线程中来实现。


(3)方法三:


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);
        }
    }
}


分析:
利用类成员变量被所有类的实例所共享这一特性,因此可以将lock用静态成员对象来实现。


(四)方法四:


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);
        }
    }
}


分析:实例方法中加入sychronized关键字封锁的是this对象本身,而在静态方法中加入sychronized关键字封锁的就是类本身。静态方法是所有类实例对象所共享的,因此线程对象在访问此静态方法时是互斥访问的,从而可以实现线程的同步
0 0
原创粉丝点击