标签:pac code das main 占用 机制 加锁 seconds strong
1 public synchronized void output1() { 2 for(int i=0;i<10;i++) { 3 System.out.println(Thread.currentThread().getName()); 4 try { 5 Thread.sleep(100); 6 } catch (InterruptedException e) { 7 e.printStackTrace(); 8 } 9 } 10 }
Synchronized test
thread1
thread1
thread1
thread1
thread1
thread1
thread1
thread1
thread1
thread1
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
1 public void output2() { 2 for(int i=0;i<1;i++) { 3 System.out.println(Thread.currentThread().getName() + "---read"); 4 } 5 synchronized(Synchronized_test.syn) { 6 for(int i=0;i<10;i++) { 7 System.out.println(Thread.currentThread().getName() + "--- write"); 8 try { 9 Thread.sleep(100); 10 } catch (InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } 14 } 15 }
Synchronized test
thread1---read
thread2---read
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2—write
1 @Override 2 public void run() { 3 lock.lock(); 4 for(int i=1;i<10;i++) { 5 System.out.println(Thread.currentThread().getName()); 6 try { 7 Thread.sleep(100); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 } 12 lock.unlock(); 13 }
lock test Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-0 Thread-1 Thread-1 Thread-1 Thread-1 Thread-1 Thread-1 Thread-1 Thread-1 Thread-1
1 public class Lock_interrupt_test implements Runnable { 2 private Lock lock = null; 3 public Lock_interrupt_test() { 4 lock = new ReentrantLock(); 5 } 6 7 @Override 8 public void run() { 9 try { 10 lock.lockInterruptibly(); 11 for (int i = 0; i < 10; i++) { 12 System.out.println(Thread.currentThread().getName()); 13 Thread.sleep(100); 14 } 15 } catch (InterruptedException e1) { 16 e1.printStackTrace(); 17 } finally { 18 lock.unlock(); 19 } 20 } 21 public static void main(String[] args) { 22 System.out.println("lock interrupt test"); 23 Lock_interrupt_test lock_interrupt_test = new 24 Lock_interrupt_test(); 25 Thread thread1 = new Thread(lock_interrupt_test, "thread1"); 26 Thread thread2 = new Thread(lock_interrupt_test, "thread2"); 27 thread1.start(); 28 thread2.start(); 29 try { 30 Thread.sleep(400); 31 } catch (InterruptedException e) { 32 e.printStackTrace(); 33 } 34 State state1 = thread1.getState(); 35 Thread thread = state1.toString().equals("TIMED_WAITING")? 36 thread1:thread2; 37 System.out.println("interrupt " + thread.getName()); 38 thread.interrupt(); 39 } 40 }
lock interrupt test thread1 thread1 thread1 thread1 interrupt thread1 java.lang.InterruptedException: sleep interrupted at java.base/java.lang.Thread.sleep(Native Method) at lock_type.Lock_interrupt_test.run(Lock_interrupt_test.java:21) at java.base/java.lang.Thread.run(Thread.java:844) thread2 thread2 thread2 thread2 thread2 thread2 thread2 thread2 thread2 thread2
1 @Override 2 public void run() { 3 try { 4 if (lock.tryLock(400,TimeUnit.MILLISECONDS)) { 5 System.out.println(Thread.currentThread().getName() + " have get the lock"); 6 for (int i = 1; i < 10; i++) { 7 System.out.println(Thread.currentThread().getName()); 8 try { 9 Thread.sleep(100); 10 } catch (InterruptedException e) { 11 e.printStackTrace(); 12 } 13 } 14 System.out.println(Thread.currentThread().getName() + " have 15 return the lock"); 16 lock.unlock(); 17 } 18 else { 19 for (int i = 1; i < 10; i++) { 20 System.out.println(Thread.currentThread().getName() + " locking"); 21 try { 22 Thread.sleep(100); 23 } catch (InterruptedException e) { 24 e.printStackTrace(); 25 } 26 } 27 } 28 } catch (InterruptedException e) { 29 e.printStackTrace(); 30 } 31 }
lock try test Thread-0 have get the lock Thread-0 Thread-0 Thread-0 Thread-0 Thread-1 locking Thread-0 Thread-1 locking Thread-0 Thread-1 locking Thread-0 Thread-1 locking Thread-0 Thread-1 locking Thread-0 Thread-1 locking Thread-0 have return the lock Thread-1 locking Thread-1 locking Thread-1 locking
1 @Override 2 public void run() { 3 lock.readLock().lock(); 4 for(int i=0;i<10;i++) { 5 System.out.println(Thread.currentThread().getName() + "---read"); 6 try { 7 Thread.sleep(100); 8 } catch (InterruptedException e) { 9 e.printStackTrace(); 10 } 11 } 12 lock.readLock().unlock(); 13 lock.writeLock().lock(); 14 for(int i=0;i<10;i++) { 15 System.out.println(Thread.currentThread().getName() + "---write"); 16 try { 17 Thread.sleep(100); 18 } catch (InterruptedException e) { 19 e.printStackTrace(); 20 } 21 } 22 lock.writeLock().unlock(); 23 }
lock read write test
thread2---read
thread1---read
thread1---read
thread2---read
thread1---read
thread2---read
thread2---read
thread1---read
thread1---read
thread2---read
thread1---read
thread2---read
thread1---read
thread2---read
thread1---read
thread2---read
thread2---read
thread1---read
thread2---read
thread1---read
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread1---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
thread2---write
标签:pac code das main 占用 机制 加锁 seconds strong
原文地址:https://www.cnblogs.com/xiatianyu/p/9038993.html