ReentrantLock
练习一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class ReentrantLock1 { synchronized void m1 () { for (int i = 0 ; i < 10 ; i++) { try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(i); } } synchronized void m2 () { System.out.println("m2..." ); } public static void main (String[] args) { ReentrantLock1 rL = new ReentrantLock1 (); new Thread (rL::m1).start(); try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } new Thread (rL::m2).start(); } }
练习二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 public class ReentrantLock2 { Lock lock = new ReentrantLock (); void m1 () { try { lock.lock(); for (int i = 0 ; i < 10 ; i++) { TimeUnit.SECONDS.sleep(1 ); System.out.println(i); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } void m2 () { lock.lock(); System.out.println("m2..." ); lock.unlock(); } public static void main (String[] args) { ReentrantLock2 rL = new ReentrantLock2 (); new Thread (rL::m1).start(); try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } new Thread (rL::m2).start(); } }
练习三:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 public class ReentrantLock3 { Lock lock = new ReentrantLock (); void m1 () { try { lock.lock(); for (int i = 0 ; i < 10 ; i++) { TimeUnit.SECONDS.sleep(1 ); System.out.println(i); } } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } } void m2 () { boolean locked = false ; try { lock.tryLock(5 , TimeUnit.SECONDS); System.out.println("m2..." + locked); } catch (InterruptedException e) { e.printStackTrace(); } finally { if (locked) lock.unlock(); } } public static void main (String[] args) { ReentrantLock3 rL = new ReentrantLock3 (); new Thread (rL::m1).start(); try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } new Thread (rL::m2).start(); } }
练习四:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 public class ReentrantLock4 { public static void main (String[] args) { Lock lock = new ReentrantLock (); boolean locked = false ; Thread t1 = new Thread (() -> { try { lock.lock(); System.out.println("t1 start" ); TimeUnit.SECONDS.sleep(Integer.MAX_VALUE); System.out.println("t1 end" ); } catch (InterruptedException e) { System.out.println("interrupted! t1" ); } finally { lock.unlock(); } }); t1.start(); Thread t2 = new Thread (() -> { try { lock.lockInterruptibly(); System.out.println("t2 start" ); TimeUnit.SECONDS.sleep(5 ); System.out.println("t2 end" ); } catch (InterruptedException e) { System.out.println("interrupted! t2" ); } finally { if (locked) lock.unlock(); } }); t2.start(); try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } t2.interrupt(); } }
练习五:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 public class ReentrantLock5 extends Thread { public static ReentrantLock lock = new ReentrantLock (true ); @Override public void run () { for (int i = 0 ; i < 100 ; i++) { lock.lock(); try { System.out.println(Thread.currentThread().getName() + "获得锁" ); } finally { lock.unlock(); } } } public static void main (String[] args) { ReentrantLock5 rL = new ReentrantLock5 (); Thread t1 = new Thread (rL); Thread t2 = new Thread (rL); t1.start(); t2.start(); } }
ThreadLocal
练习一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 public class ThreadLocal1 { volatile static Person p = new Person (); public static void main (String[] args) { new Thread (() -> { try { TimeUnit.SECONDS.sleep(2 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(p.name); }).start(); new Thread (() -> { try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } p.name = "lisi" ; }).start(); } } class Person { String name = "zhangsan" ; }
练习二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class ThreadLocal2 { static ThreadLocal <Person> tl = new ThreadLocal <>(); public static void main (String[] args) { new Thread (() -> { try { TimeUnit.SECONDS.sleep(2 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + tl.get()); }, "t1" ).start(); new Thread (() -> { try { TimeUnit.SECONDS.sleep(1 ); } catch (InterruptedException e) { e.printStackTrace(); } tl.set(new Person ()); System.out.println(Thread.currentThread().getName() + ":" + tl.get().name); }, "t2" ).start(); } static class Person { String name = "zhangsan" ; } }
线程安全的单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class Singleton { private Singleton () { System.out.println("single" ); } private static class Inner { private static Singleton s = new Singleton (); } private static Singleton getInstance () { return Inner.s; } public static void main (String[] args) { Thread [] ths = new Thread [200 ]; for (int i = 0 ; i < ths.length; i++) { ths[i] = new Thread (() -> { Singleton.getInstance(); }); } List <Thread> threads = Arrays.asList(ths); threads.forEach(thread -> thread.start()); } }
源码:https://github.com/V-Vincen/threads/tree/master/src/main/java/com/example/_exercise
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !