码迷,mamicode.com
首页 > 编程语言 > 详细

多线程日记(17.5.2two)synchronized的基本规则

时间:2017-05-04 00:13:26      阅读:272      评论:0      收藏:0      [点我收藏+]

标签:color   imp   first   线程   http   dex   日记   同步代码块   zed   

synchronized关键字

1)当有一条线程访问某个对象的synchronized的方法或代码块时,其它线程进行访问将会被阻塞;

2)当有一条线程访问某个对象的synchronized的方法或代码块时,其它线程访问该对象的非同步代码块时不会被阻塞;

3)当有一条线程访问某个对象的synchronized的方法或代码块时,其它线程访问该对象的其它同步代码块时将会被阻塞。

第一条规则

public class Test {
    public static void main(String[]args){
        //create object for the two threads
        NewThread obj=new NewThread();
        //create new thread
        Thread thread1=new Thread(obj,"t1");
        Thread thread2=new Thread(obj,"t2");
        thread1.start();
        thread2.start();
    }
}
class NewThread implements Runnable{
    @Override
    public void run(){
        synchronized(this){
            try{
                for(int i=0;i<3;i++){
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName()+":"+(i+1));
                }
            }catch(InterruptedException e){
                System.out.println("error");
            }
        }
    }
    
}

第二条规则:

public class Test {
    public static void main(String[]args){
        final Demo demo=new Demo();
        //create first thread
        Thread thread1=new Thread(new Runnable(){
            @Override
            public void run(){
                demo.synchronizedThread();
            }
        },"t1");
        //create second thread
        Thread thread2=new Thread(new Runnable(){
            @Override
            public void run(){
                demo.unsynchronizedThread();
            }
        },"t2");
        thread1.start();
        thread2.start();
    }
}
class Demo{
    //synchronized method
    public void synchronizedThread(){
        synchronized(this){
            try{
                for(int i=0;i<3;i++){
                    System.out.println(Thread.currentThread().getName()+":"+(i+1));
                    Thread.sleep(100);
                }
            }catch(InterruptedException e){
                System.out.println("error");
            }
        }
    }
    //unsynchronized method
    public void unsynchronizedThread(){
        try{
            for(int i=0;i<3;i++){
                System.out.println(Thread.currentThread().getName()+":"+(i+1));
                Thread.sleep(100);
            }
        }catch(InterruptedException e){
            System.out.println("error");
        }

    }
}

第三条规则:

public class Test {
    public static void main(String[]args){
        final Demo demo=new Demo();
        //create first thread
        Thread thread1=new Thread(new Runnable(){
            @Override
            public void run(){
                demo.synchronizedThread();
            }
        },"t1");
        //create second thread
        Thread thread2=new Thread(new Runnable(){
            @Override
            public void run(){
                demo.samesynchronizedThread();
            }
        },"t2");
        thread1.start();
        thread2.start();
    }
}
class Demo{
    //synchronized method
    public void synchronizedThread(){
        synchronized(this){
            try{
                for(int i=0;i<3;i++){
                    System.out.println(Thread.currentThread().getName()+":"+(i+1));
                    //sleep(100)
                    Thread.sleep(100);
                }
            }catch(InterruptedException e){
                System.out.println("error");
            }
        }
    }
    //synchronized method
    public void samesynchronizedThread(){
        synchronized(this){
            try{
                for(int i=0;i<3;i++){
                    System.out.println(Thread.currentThread().getName()+":"+(i+1));
                    //sleep(100)
                    Thread.sleep(100);
                }
            }catch(InterruptedException e){
                System.out.println("error");
            }
        }
    }
}

synchronized的方法和代码块

方法:public synchronized void method1(){}

代码块:class Lei{synchronized(this)}

参考自:http://www.cnblogs.com/skywang12345/p/3479202.html#p3

多线程日记(17.5.2two)synchronized的基本规则

标签:color   imp   first   线程   http   dex   日记   同步代码块   zed   

原文地址:http://www.cnblogs.com/levi-ji/p/6804512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!