Synchronized Review
1, multi-threaded programming template
(1) Operating resource thread
(2) high cohesion and low coupling
2, implementation steps
(1) create a resource class
(2) the resource class to create some type of synchronization code block
3, an example: Ticketing
LOCK Interface
Lock implementations provide more extensive locking operations than using synchronized methods and statements available. They allow a more flexible structure, may have very different properties, and may support a plurality of objects associated conditions.
1, Lock interface implementation ReentrantLock reentrant lock
(1) How to use?
class X { private final ReentrantLock lock = new ReentrantLock(); // ... public void m() { lock.lock(); // block until condition holds try { // ... method body } finally { lock.unlock() } } }
2, create a thread way
(1) inherited Thread
public class SaleTicket the extends the Thread the Java is a single inheritance, valuable resources, use Interface
(2)new Thread()
Thread t1 = new Thread(); t1 .start();
(3) Thread (Runnable target, String name) "is best to use a third"
3, to achieve Runnable method
(1) New class implements interfaces runnable
class MyThread the implements Runnable // create a new class that implements the interface runnable new new the Thread ( new new MyThread, ...) This method will add class, there are newer and better methods
(2) anonymous inner classes
new the Thread ( new the Runnable () { @Override public void RUN () { } }, "Thread your name" ) .start (); this method does not create a new class, new interfaces can be
(3) lambda expression
new new the Thread (() -> { }, "Thread your name" ) .start (); This code more concise refining method
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--)+"\t 还剩number); } }*/ // Lock implementations provide more extensive locking operations // than can be obtained using synchronized methods and statements. Private Lock Lock = new new of ReentrantLock (); // List List new new = the ArrayList () public void Sale () { Lock.lock (); the try { IF (Number> 0 ) { System.out.println (Thread.currentThread () .getName () + "sell" + (number -) + " \ t left Number); } } the catch (Exception E) { e.printStackTrace (); } the finally { lock.unlock (); } } } / ** * * @Description: a ticket program ticket out of 0 votes @author xiale * note: J inside how more than one thread knitting - on 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