Common situations of thread lock

Several situations of thread lock

  1. Two common synchronization methods, two threads:

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

    Print output result:

    one 
    two
    
  2. Add Thread.sleep(3000) in the getOne method:

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

    Print output result:

    one
    two
    
  3. Add the asynchronous method getThree, start the thread and call:

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

    Print output result:

    three
    one
    two
    
  4. Two synchronization methods, two Number objects, respectively start threads to call methods in different Numbers:

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

    Print output result:

    two
    one
    
  5. Modify getOne as a static synchronization method, and getTwo as a normal synchronization method:

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

    Print output result:

    two
    one
    
  6. Set both getOne and getTwo as static synchronization methods:

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

    Print output result:

    one
    two
    
  7. getOne is a static synchronization method, and getTwo is a normal synchronization method. The methods are called using two Number objects:

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

    Print output result:

    two
    one
    
  8. Set both getOne and getTwo as static synchronization methods, and call the methods with two Number objects:

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

    Print output result:

    one
    two
    

Guess you like

Origin blog.csdn.net/Java_lover_zpark/article/details/89470666