线程锁的常见几种情况

线程锁的几种情况

  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
    

猜你喜欢

转载自blog.csdn.net/Java_lover_zpark/article/details/89470666