Several situations of thread lock
-
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
-
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
-
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
-
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
-
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
-
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
-
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
-
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