package test_list;
public class Test_List {
public static void main(String[] args) { final Object resource_one = "resourceone"; final Object resource_two = "resourcetwo"; Thread thread1 = new Thread() {
public void run() { synchronized (resource_one) { System.out.println("線程1鎖定了resource_one"); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } synchronized (resource_two) { System.out.println("線程1鎖定了resource_two"); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } }; Thread thread2 = new Thread() {
public void run() { synchronized (resource_two) { System.out.println("線程2鎖定了resource_two"); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); }
synchronized (resource_one) { System.out.println("線程2鎖定了resource_one"); try { Thread.sleep(1000); } catch (InterruptedException ex) { ex.printStackTrace(); } } } } }; thread1.start(); thread2.start();
} }
|