线程锁的常见几种情况

线程锁的几种情况

  1. 两个普通同步方法,两个线程:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getTwo();
    			}
    		}).start();
    	}
    }
    class Number{
    	public synchronized void getOne(){
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    }
    

    打印输出结果:

    one 
    two
    
  2. 在getOne方法中添加Thread.sleep(3000):

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getTwo();
    			}
    		}).start();
    	}
    }
    class Number{
    	public synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    }
    

    打印输出结果:

    one
    two
    
  3. 添加非同步方法getThree ,开启线程并调用:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getTwo();
    			}
    		}).start();
    		new Thread(new Runnable() {
    			public void run() {
    				number.getThree();
    			}
    		}).start();
    	}
    }
    class Number{
    	public synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    three
    one
    two
    
  4. 两个同步方法,两个Number对象,分别开启线程调用不同的Number中的方法:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		final Number number2=new Number();
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number2.getTwo();
    			}
    		}).start();
    		
    	}
    }
    class Number{
    	public synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    two
    one
    
  5. 将getOne修改为静态同步方法,getTwo为普通同步方法:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getTwo();
    			}
    		}).start();
    		
    	}
    }
    class Number{
    	public static synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    two
    one
    
  6. 将getOne和getTwo都设置为静态同步方法:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getTwo();
    			}
    		}).start();
    		
    	}
    }
    class Number{
    	public static synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public static synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    one
    two
    
  7. getOne为静态同步方法,getTwo为普通同步方法,利用两个Number对象分别调用方法:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		final Number number2 = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number2.getTwo();
    			}
    		}).start();
    		
    	}
    }
    class Number{
    	public static synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    two
    one
    
  8. 将getOne和getTwo都设置为静态同步方法,用两个Number对象分别调用方法:

    public class TestSynchronized {
    	public static void main(String[] args) throws InterruptedException {
    		final Number number = new Number();
    		final Number number2 = new Number();
    		
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number.getOne();
    			}
    		}).start();
    		Thread.sleep(100);
    		//启动一个线程
    		new Thread(new Runnable() {
    			public void run() {
    				number2.getTwo();
    			}
    		}).start();
    		
    	}
    }
    class Number{
    	public static synchronized void getOne(){
    		try {
    			Thread.sleep(3000);
    		} catch (InterruptedException e) {
    			e.printStackTrace();
    		}
    		System.out.println("one...");
    	}
    	public static synchronized void getTwo(){
    		System.out.println("two...");
    	}
    	public void getThree(){
    		System.out.println("three...");
    	}
    }
    

    打印输出结果:

    one
    two
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值