public class SynchronizedField { private static Double balance = 100.0; public static void main(String[] args) { lockField(); } public static void lockField() { Thread withDrawThread = new WithdrawThread(10.0); Thread depositThread = new Thread(new DepositThread(10.0)); withDrawThread.start(); depositThread.start(); } private static final class WithdrawThread extends Thread { private Double amount; public WithdrawThread(Double amount) { this.amount = amount; } public void run() { synchronized (balance) { System.out.println("Before withdrawing " + amount + ", the balance is " + balance); balance -= amount; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Withdrew " + amount + ", the balance is " + balance); } } } private static final class DepositThread implements Runnable { private Double amount; public DepositThread(Double amount) { this.amount = amount; } public void run() { synchronized (balance) { System.out.println("Before depositing " + amount + ", the balance is " + balance); balance += amount; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Deposited " + amount + ", the balance is " + balance); } } } }
public class SynchronizedMethod { private static Double balance = 100.0; public static void main(String[] args) { lockMethod(); } public static void lockMethod() { Thread depositThread1 = new Thread(new DepositThread(10.0)); Thread depositThread2 = new Thread(new DepositThread(10.0)); depositThread1.start(); depositThread2.start(); } private static final class DepositThread implements Runnable { private Double amount; public DepositThread(Double amount) { this.amount = amount; } public void run() { deposit(amount); } } public static synchronized void deposit(Double amount) { System.out.println("Before depositing " + amount + ", the balance is " + balance); balance += amount; try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Deposited " + amount + ", the balance is " + balance); } }
public class LearnVolatile { public synchronized void SyncForInstance() { for(int i=0; i<10; i++) { System.out.println("SyncForInstance"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } public static synchronized void SyncForClass() { for(int i=0; i<10; i++) { System.out.println("SyncForClass"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } static Thread t1 = new Thread() { public void run() { SyncForClass(); } }; static Thread t2 = new Thread() { public void run() { new LearnVolatile().SyncForInstance(); } }; public static void main(String[] args) { t1.start(); t2.start(); } }
SyncForClass SyncForInstance SyncForClass SyncForInstance ……
public class LearnVolatile { private static Lock lock = new ReentrantLock(); public void SyncForInstance() { try { lock.lock(); for(int i=0; i<10; i++) { System.out.println("SyncForInstance"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } } public static void SyncForClass() { try { lock.lock(); for(int i=0; i<10; i++) { System.out.println("SyncForClass"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } finally { lock.unlock(); } } static Thread t1 = new Thread() { public void run() { SyncForClass(); } }; static Thread t2 = new Thread() { public void run() { new LearnVolatile().SyncForInstance(); } }; public static void main(String[] args) { t1.start(); t2.start(); } }
SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForClass SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance SyncForInstance
public class LearnThread { public static void main(String[] args) { Thread t1 = new Thread() { public void run() { System.out.println("...."); } }; t1.start(); t1.start(); } }
.... Exception in thread "main" java.lang.IllegalThreadStateException at java.lang.Thread.start(Thread.java:704) at com.jesse.core.learnvolatile.LearnThread.main(LearnThread.java:12)
public class DeadLock { private static Lock lockA = new ReentrantLock(); private static Lock lockB = new ReentrantLock(); public static void main(String[] args) { Thread t1 = new Thread() { public void run() { try { lockA.lock(); System.out.println("t1 get lockA"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("t1 is waiting lockB"); lockB.lock(); } finally { lockA.unlock(); lockB.unlock(); } } }; Thread t2 = new Thread() { public void run() { try { lockB.lock(); System.out.println("t2 got lockB"); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("t2 is waiting for lockA"); lockA.lock(); } finally { lockB.unlock(); lockA.unlock(); } } }; t1.start(); t2.start(); } }
t2 got lockB t1 get lockA t2 is waiting for lockA t1 is waiting for lockB
0票
开心
0票
板砖
0票
感动
0票
有用
0票
疑问
0票
难过
0票
无聊
0票
震惊
顶
踩