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

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

转载于:https://www.cnblogs.com/levi-ji/p/6804512.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值