What is concurrency

Concurrency in the operating system refers to a period of time when several programs are running between start and finish, and these programs are running on the same processor, but only one program is running on the processor at any point in time.Copy the code

Why concurrency

  • Improve CPU efficiency
  • Reduce system response time
  • Improve system fault tolerance

Concurrent Operation (JUC)

Use of threads

1. Implement the Runnable interface

public class TestRunnable implements Runnable{ @Override public void run(){ //... } // Create a Thread instance with Runnable, Public static void main(String[] args){TestRunnable tr = new TestRunnable(); Thread thread = new Thread(tr); thread.start(); }}Copy the code

2. Inherit the Thread class

public class TestThread extends Thread{ public void run(){ //... } public static void main(String[] args){TestThread tt = new TestThread(); tt.start(); }}Copy the code

3. Implement the Callable interface

Public class TestCallable implements Callable{// The returned value can be any reference type value. For example, Integer @override public Integer run(){//... return 123; } // In contrast to Runnable, Callable can have a return value, which is encapsulated by FutureTask. public static void main(String[] args){ TestCallable tr = new TestCallable(); FutureTask<Integer> fu = new FutureTask<>(tr); Thread thread = new Thread(fu); thread.start(); System.out.println(fu.get()); }}Copy the code

Implement interface vs inheritance Thread

1.Java does not support multiple inheritance, but can implement multiple interfaces. 2

Thread -> Lock (mutex synchronization)

Java provides two locking mechanisms to control mutually exclusive access by multiple threads to a shared resource

1.Sychronized

use

  • Synchronized code block
public void func() { synchronized (this) { // ... }}Copy the code
  • Synchronized methods
public synchronized void func () {
    // ...
}
Copy the code
  • Synchronization class
Public void func () {/ / synchronization of a class of synchronized (SynchronizedExample. Class) {/ /... }}Copy the code
  • Synchronous static method
public synchronized static void fun() {
    // ...
}
Copy the code

2.ReentrantLock

Public class TestLock {// instance ReentrantLock private Lock Lock = new ReentrantLock(); public void func() { lock.lock(); try { for (int i = 0; i < 10; i++) { System.out.print(i + " "); } } finally { lock.unlock(); // Make sure the lock is released to avoid deadlocks. }}}Copy the code

Both are

  • Lock implementation: the former is the JVM implementation, the latter is the JDK API
  • Performance: Newer versions of Java have Sychronized optimizations, such as spinlocks, which are roughly the same
  • Interruptible wait: if the thread currently holding the lock does not release the lock, the latter can abandon the wait, but the former cannot
  • Fair lock: the former is not fair lock, the latter can be fair or not fair

Use options: ReentrantLock’s advanced features are preferred, otherwise Sychronized is preferred, since the former is implemented by the native JVM and will be supported by all versions of Java. In addition, Sychronized does not care about lock release, so there is no need to worry about deadlock

Thread -> state

  • New: The thread has not been started after creation
  • Runnable: Can be run, depending on the underlying operating system scheduling
  • Bolcked: A resource is blocked when another thread occupies the resource
  • Wait indefinitely: Wait for another thread to explicitly wake up.
  • Finite wait: the system wakes up automatically after a certain amount of time without waiting explicitly for another thread to wake up.
  • Death: This can happen when the thread terminates the task, or when an exception is raised.

Java SE 9 Enum Thread.State