JUC-LOCK接口

Synchronized

1、多线程编程模版上

(1)线程 操作 资源类

(2)高内聚低耦合

2、实现步骤

(1)创建资源类

(2)资源类里创建同步方法,同步代码块

3、例子:卖票

 LOCK 接口

锁实现提供了比使用同步方法和语句可以获得的更广泛的锁操作。它们允许更灵活的结构,可能具有非常不同的属性,并且可能支持多个关联的条件对象。

1、Lock接口的实现   ReentrantLock可重入锁

(1)如何使用?

class X {
   private final ReentrantLock lock = new ReentrantLock();
   // ...
   public void m() {
     lock.lock();  // block until condition holds
     try {
       // ... method body
     } finally {
       lock.unlock()
     }
   }
 }

 2、创建线程方式 

(1)继承Thread

public  class SaleTicket extends Thread

java是 单继承,资源宝贵,要用接口方式

 (2)new Thread()

Thread  t1  =  new  Thread(); 
t1 .start(); 

 (3)Thread(Runnable target, String name) 「最好使用第三种」

3、实现Runnable方法

(1)新建类实现runnable接口

class   MyThread   implements   Runnable //新建类实现runnable接口   
new  Thread( new   MyThread ,...) 


这种方法会新增类,有更新更好的方法 

(2)匿名内部类

new  Thread(
  new Runnable() {   @Override    public void run() {
  } },
"your thread name" ).start(); 这种方法不需要创建新的类,可以new接口   

(3)lambda表达式

new  Thread(() -> { 
  
 },  "your thread name" ).start(); 
 
  这种方法代码更简洁精炼 
package com.atguigu.thread; 
  
import java.util.concurrent.locks.Lock; 
import java.util.concurrent.locks.ReentrantLock; 
  
class Ticket //实例例eld +method 
{ 
 private int number=30; 
/* //1同步 public synchronized void sale()  
 {//2同步  synchronized(this) {} 
  if(number > 0) { 
    System.out.println(Thread.currentThread().getName()+"卖出"+(number--)+"	 还剩number); 
   } 
 }*/ 
  
// Lock implementations provide more extensive locking operations 
// than can be obtained using synchronized methods and statements.  
 private Lock lock = new ReentrantLock();//List list = new ArrayList() 
  
 public void sale()  
 { 
   lock.lock(); 
    
   try { 
    if(number > 0) { 
     System.out.println(Thread.currentThread().getName()+"卖出"+(number--)+"	 还剩number); 
    } 
   } catch (Exception e) { 
    e.printStackTrace(); 
   } finally { 
    lock.unlock(); 
   } 
    
 } 
  
} 
  
  
/** 
 *  
 * @Description:卖票程序个售票出  0张票 
 @author xiale 
 * 笔记:J里面如何 1 多线程编-上 
    1.1 线程  (资里源类 *   1.2 高内聚 / 
public class SaleTicket  
{ 
 public static void main(String[] args)//main所有程序 
   Ticket ticket = new Ticket(); 
   //Thread(Runnable target, String name) Allocates a new Thread object. 
    
  
    
 new Thread(() -> {for (int i = 1; i < 40; i++)ticket.sale();}, "AA").start(); 
 new Thread(() -> {for (int i = 1; i < 40; i++)ticket.sale();}, "BB").start(); 
 new Thread(() -> {for (int i = 1; i < 40; i++)ticket.sale();}, "CC").start(); 
  
    
    
    
/*  new Thread(new Runnable() { 
    @Override 
    public void run()  
    { 
     for (int i = 1; i <=40; i++)  
     { 
        
       ticket.sale(); 
     } 
    } 
   }, "AA").start(); 
    
   new Thread(new Runnable() { 
    @Override 
    public void run()  
    { 
     for (int i = 1; i <=40; i++)  
     { 
       ticket.sale(); 
     } 
    } 
   }, "BB").start(); 
   new Thread(new Runnable() { 
    @Override 
    public void run()  
    { 
     for (int i = 1; i <=40; i++)  
     { 
       ticket.sale(); 
     } 
    } 
   }, "CC").start(); 
   */ 
 } 
} 
  
//1 class MyThread implements Runnable 
  
//2 匿名内部类 
  
//3 laExpress 
  
原文地址:https://www.cnblogs.com/minmin123/p/11412154.html