April 24, 2018 Java

Source: Internet
Author: User
Tags finally block thread class volatile

Multithreading

There is only one purpose of multithreading, which is to make better use of CPU resources, because all multithreaded code can be implemented by single thread. Say this is only half right, because the reaction "multi-Role" of the program code, at least each role to give him a thread bar, or even the actual scene can not be simulated, of course, it is not possible to use a single thread to achieve: for example, the most common "producer, consumer model."

Many people are not clear about some of these concepts, such as synchronization, concurrency and so on, let us first set up a data dictionary, in order to avoid misunderstanding.

    • Multithreading: Refers to the program (a process) when the runtime produces more than one thread
    • Parallel and Concurrency:
      • Parallel: Multiple CPU instances or multiple machines executing a processing logic at the same time are true at the same time.
      • Concurrency: Through the CPU scheduling algorithm, let the user appear to execute simultaneously, actually from the CPU operation level is not real at the same time. Concurrency often has a common resource in the scene, so for this common resource is often a bottleneck, we will use TPS or QPS to reflect the processing power of the system.

Concurrency and parallelism
    • Thread safety: Often used to depict a piece of code. In the case of concurrency, the code is multithreaded and the scheduling order of the threads does not affect any results. This time using multi-threading, we only need to focus on the system's memory, the CPU is not enough. In turn, thread insecurity means that the scheduling order of threads affects the end result, such as no transaction transfer code:
      void TransferMoney (user from, user to, float amount) {  To.setmoney (to.getbalance () + amount);  From.setmoney (from.getbalance ()-amount);}
    • Synchronization: Synchronization in Java refers to the use of human control and scheduling, to ensure that multi-threaded access to shared resources become thread-safe, to ensure the accuracy of the results. As the above code simply adds @synchronized keywords. In ensuring that the results are accurate at the same time, improve performance, is a good program. Thread safety has a higher priority than performance.

All right, let's get started. I'm going to split into several points to summarize what's involved in Multithreading:

    1. Tie the horse: The State of the thread
    2. Internal organs Heart: The method of each object (mechanism)
    3. Chang Quan: Basic Threading Class
    4. Nine Yin Canon: Advanced Multithreading Control class
Tie the horse: The State of the thread

Let's take two pictures first:


Thread state
Thread state transitions


Various states at a glance, it is worth mentioning that the "blocked" this state:
A thread may experience a blocking (Blocked) condition during running

    1. Call the Join () and sleep () methods, the sleep () time ends or is interrupted, the join () is interrupted, and the IO completion will return to the runnable state, waiting for the JVM to dispatch.
    2. Call Wait () so that the thread is in the waiting pool (wait blocked pool) until notify ()/notifyall (), the thread is woken up and placed into the lock pool (lock blocked), Releasing a sync lock to bring the thread back to the operational state (Runnable)
    3. The line loads Sync Lock (Synchronized) for the running state enters (lock blocked pool) and the Sync Lock is released into the operational state (Runnable).

In addition, the thread in the runnable state is in the thread being dispatched, at which point the scheduling order is not necessarily. The yield method in the thread class allows a thread of the running state to be transferred to Runnable.

Internal organs Heart: The method of each object (mechanism)

Synchronized, wait, notify is a synchronization tool that any object has. Let's get to know them first.


Monitor


They are manual thread scheduling tools that are applied to synchronization problems. Speaking of its essence, the concept of monitor must be clarified first, and every object in Java has a monitor to monitor the re-entry of concurrent code. The monitor does not work when it is not multithreaded, and the monitor works if it is within the synchronized range.

The wait/notify must exist in the synchronized block. Also, these three keywords are for the same monitor (the monitor for an object). This means that after wait, other threads can go into the synchronization block execution.

When a code does not hold the monitor's right to use (in 5 of the State, i.e. out of sync block) to wait or notify, it throws java.lang.IllegalMonitorStateException. It is also included in the synchronized block to invoke the wait/notify of another object, because different objects have different monitors, and this exception is thrown.

To speak again of usage:

  • Synchronized used alone:
    • Code block: As below, in a multithreaded environment, the method in the synchronized block acquires the monitor of the lock instance, and if the instance is the same, only one thread can execute the block content
      public class Thread1 implements Runnable {   Object lock;   public void Run () {         synchronized (lock) {         : Do Something       }}}   
    • Directly used in the method: equivalent to the above code with lock locking effect, actually obtained is the Thread1 class of Monitor. Further, if the static method is decorated, all instances of the class are locked.
      public class Thread1 implements Runnable {public   synchronized void Run () {          : Do Something   }}
  • Synchronized, wait, notify combination: Typical scenario producer consumer issues

    /**   * The product produced by the producer is given to the clerk */public  synchronized Void Produce ()  {      if (this.product >= max_product)      {          Try          {              wait ();                SYSTEM.OUT.PRINTLN ("Product is full, please wait for reproduction");          }          catch (interruptedexception e)          {              e.printstacktrace ();          }          return;      }      this.product++;      System.out.println ("producer Production" + This.product + "products.");      Notifyall ();   Notifies the waiting area that the consumer can take out the product  }  /**   * The consumer takes the product from the clerk *   /public  synchronized void consume ()  {      if (this.product <= min_product)      {          Try           {              wait ();               System.out.println ("Out of stock, fetch later");          }           catch (interruptedexception e)           {              e.printstacktrace ();          }          return;      }      System.out.println ("The consumer took the first" + This.product + "products.");      this.product--;      Notifyall ();   Inform the producers waiting to be ready to produce the product.  }
    Volatile

    Multi-threaded memory model: Main Memories (memory), working memory (line stacks), when processing data, the thread will load the value from memory to the local stack, complete operation and then save back ( The role of volatile keywords: every action against the variable fires a load and save.


Volatile

Variables used for multithreading, if not volatile or final decorated, are likely to produce unpredictable results (another thread modifies the value, but then a thread sees the value before it is modified). In fact, the same property itself has only one copy of the same instance. However, multithreading is the cache value, in essence, volatile is not to cache, directly take value. Adding volatile in the case of thread security can compromise performance.

Chang Quan: Basic Threading Class

The basic threading class refers to the thread class, the Runnable interface, the callable interface
The thread class implements the Runnable interface, starting a thread's method:

MyThread my = new MyThread (); My.start ();

Thread Class related methods:

The current thread can transfer CPU control to allow other ready state threads to run (toggle) public static Thread.yield ()//pause for a while public static thread.sleep ()  //    Calling Other.join () in one thread waits for the other to finish before continuing with this thread. Public join ()//After both functions can be interrupted public interrupte ()

about interrupts : it does not break a running thread like the Stop method does. The thread periodically detects the interrupt identity bit to determine if the thread should be interrupted (the interrupt identity value is true). The terminal only affects the wait state, sleep state, and join status. The interrupted thread throws interruptedexception.
Thread.interrupted () checks whether the current thread has broken, returns a Boolean
Synchronized cannot be interrupted in the process of being locked.

Interrupts are a state! The interrupt () method simply resets the state to true. So the normal running program does not detect the state, it will not terminate, and wait and other blocking methods will check and throw an exception. If you add while (!) in the normal running program. Thread.interrupted ()), you can also leave the code body after the interrupt

Thread class Best Practices :
When writing, it is best to set the thread name Thread.Name and set the thread group Threadgroup to facilitate management. In the event of a problem, the print line stacks (jstack-pid) can see at a glance which thread is out of the question, what the thread is doing.

How to get exceptions in a thread


You cannot use Try,catch to get exceptions in a thread runnable

Similar to Thread

Callable

Future mode: One of the concurrency patterns, which can take two forms, namely, no blocking and blocking, respectively Isdone and get. Where the future object is used to store the return value and state of the thread

Executorservice e = Executors.newfixedthreadpool (3); The Submit method has multiple parameter versions, and support for callable can also support the Runnable interface type. Future future = E.submit (New mycallable ()), Future.isdone ()//return true,false nonblocking future.get ()//return value, blocking until the thread finishes running
Nine Yin Canon: Advanced Multithreading Control class

The above is the internal strength of the heart, the next is the actual project used in the tool, Java1.5 provides a very efficient and practical multithreaded package:java.util.concurrent, provides a number of advanced tools to help developers write efficient, easy to maintain, A well-structured Java multithreaded thread.

1.ThreadLocal class

Useful: Saves the thread's independent variables. For a thread class (inherited from Thread)
When you use threadlocal to maintain variables, Threadlocal provides a separate copy of the variable for each thread that uses the variable, so each thread can independently change its own copy without affecting the copy of the other thread. Commonly used in user Login control, such as recording session information.

Implementation: Each thread holds a variable of type Treadlocalmap (the class is a lightweight map, the same as map, the difference is that the bucket is placed in the entry rather than the entry list. function is also a map. ) takes itself as key and targets as value.
The primary method is get () and set (T a), and a is returned when the set is maintained in map with a threadlocal-a,get. The threadlocal is a special container.

2. Atomic Class (Atomicinteger, Atomicboolean ... )

If you use atomic wrapper class such as Atomicinteger, or use your own to guarantee the operation of the atom, it is equivalent to synchronized

The return value is booleanatomicinteger.compareandset (int expect,int update)

This method can be used to implement optimistic locking, considering the first mentioned scenario: A to B payment of 10 yuan, a deduction of 10 yuan, B to add 10 yuan. At this time c to B2 yuan, but B's plus 10 Yuan code is about:

if (B.value.compareandset (old, value)) {   return;} else{   //try again   //If that fails, rollback and log}

Atomicreference
For Atomicreference, maybe the object will appear, the property is missing, that is oldobject = = Current, but Oldobject.getpropertya! = Current.getpropertya.
At this time, the atomicstampedreference comes in handy. This is also a very common idea, that is, add version number

3.Lock class

Lock: Inside the Java.util.concurrent package. A total of three implementations:

ReentrantLockReentrantReadWriteLock.ReadLockReentrantReadWriteLock.WriteLock

The main purpose is the same as synchronized, both are to solve the synchronization problem, dealing with resource disputes arising from the technology. Functionally similar but with some differences.

The difference is as follows:

Lock is more flexible, you can freely define the locking order of multiple locks (synchronized to follow the first order) to provide a variety of locking schemes, lock blocking, trylock non-blocking, lockinterruptily can be interrupted, There is also a trylock version with a time-out. In essence and monitor lock (that is, synchronized is the same) the greater the capacity, the greater the responsibility, must control the lock and unlock, otherwise it will lead to disaster. And the combination of the condition class. Higher performance, for example:

Synchronized and lock Performance comparison

Reentrantlock    
The meaning of reentrant is that the thread holding the lock can continue to hold it, and the number of times it is released will not really release the lock.
Use this method:

1. Start with a new instance

Static Reentrantlock r=new reentrantlock ();
2. Add lock
R.lock () or r.lockinterruptibly ();

There is a difference here, and the latter can be interrupted. When a thread is lock, the B thread is blocked, and if it is lockinterruptibly, then after calling B.interrupt (), the B thread exits the block and discards the scramble for the resource into the catch block. (If you use the latter, you must throw interruptable exception or catch)

3. Release the Lock

R.unlock ()

Must do! What must be done, put in finally. To prevent anomalies from jumping out of normal processes, leading to disaster. To add a little bit of knowledge here, finally can be trusted: After testing, even if the execution of the statement in the outofmemoryerror,finally block can be guaranteed.

Reentrantreadwritelock

Reentrant read-write lock (an implementation of a read-write lock) 

Reentrantreadwritelock lock = new Reentrantreadwritelock () Readlock r = Lock.readlock (); Writelock w = lock.writelock ();

Both have lock,unlock methods. Write, write and read mutually exclusive; Read and read are not mutually exclusive. Efficient thread-safe code that can implement concurrent reads

4. Container class

Here is a discussion of the more commonly used two:

Blockingqueueconcurrenthashmap

Blockingqueue
Blocks the queue. This class is an important class under the Java.util.concurrent package, and through the learning of the queue, this queue is a one-way queue that can add elements to the queue header and delete or remove elements at the end of the team. Similar to a pipeline, especially suitable for some scenarios of FIFO strategy. The common queue interface is mainly implemented with Priorityqueue (priority queue) and is interested in studying

Blockingqueue adds the ability to multi-threaded collaboration based on the queue:


Blockingqueue


In addition to the traditional queue function (two columns to the left of the table), blocking interface offers and poll that block the interface put and take, with the timeout feature. A put blocks when the queue is full, wakes up when there is space, and takes a block when the queue is empty until something is picked up. Used in the producer-consumer model is especially useful and is an artifact.

The common blocking queues are:

Arraylistblockingqueuelinkedlistblockingqueuedelayqueuesynchronousqueue

Concurrenthashmap
Efficient thread-safe hash map. Please compare Hashtable, Concurrenthashmap, HashMap

5. Management class

The concept of management classes is more generic and is used to manage threads that are not multithreaded in themselves, but provide some mechanism to use the tools described above to do some encapsulation.
Learn about the management classes that are worth mentioning: system-level management classes under the Threadpoolexecutor and JMX frameworks Threadmxbean
Threadpoolexecutor
If you do not understand this class, you should understand the aforementioned executorservice, it is very convenient to open a thread pool of your own:

Executorservice e = Executors.newcachedthreadpool ();    Executorservice e = Executors.newsinglethreadexecutor ();    Executorservice e = Executors.newfixedthreadpool (3);    The first is a variable size thread pool, which allocates threads according to the number of tasks,    //The second is a single thread pool, which is equivalent to Fixedthreadpool (1)    //The third is a fixed size thread pooling.    //Then run    e.execute (New Myrunnableimpl ());

This class is implemented internally through Threadpoolexecutor, which helps to understand the management of the thread pool, which is essentially a variety of implementation versions of the Threadpoolexecutor class. See Javadoc:


Threadpoolexecutor parameter explanation


Translation:

Corepoolsize: The pool inline initial and minimum values, even if it is idle, will keep the number of threads. Maximumpoolsize: Thread maximum, thread growth will never exceed this value. KeepAliveTime: When the pool thread is higher than corepoolsize, the amount of time that is spent in excess of idle threads is recycled. Before recycling is in the Wait state unit: Time unit, you can use an instance of Timeunit, such as Timeunit.milliseconds WorkQueue: Waiting place for a task to be entered (Runnable), which mainly affects the scheduling strategy, such as fairness or not, Whether it produces starvation (starving) Threadfactory: The Thread factory class, which has a default implementation, needs to implement the Threadfactory interface and pass in as a parameter if there is a custom need.
Thread pool

1. What is a thread pool: Java.util.concurrent.Executors provides an implementation of the Java.util.concurrent.Executor interface for creating a thread pool

Multithreading technology mainly solves the problem of multiple threads in the processor unit, which can significantly reduce the idle time of the processor unit and increase the throughput capacity of the processor unit.
Suppose a server takes a task to complete: T1 creates a thread time, T2 the time to execute a task in the thread, T3 destroys the threading time.

if: T1 + T3 is much larger than T2, you can use a thread pool to improve server performance.

A thread pool consists of the following four basic components:
1. Thread pool Manager (ThreadPool): Used to create and manage thread pools, including creating a thread pool, destroying the thread pool, adding new tasks;
2, worker thread (poolworker): Thread pool threads, in the absence of a task in the waiting state, you can cycle the execution of tasks;
3, Task interface (tasks): Each task must be implemented by the interface for the task of scheduling the execution of tasks, it mainly specifies the entry of the task, the completion of the task after the end of the task, the implementation of the mission status, etc.;
4. Task Queue (Taskqueue): Used to store tasks that are not processed. Provides a buffering mechanism.

Thread pooling technology is a technique that focuses on how to shorten or adjust t1,t3 time to improve the performance of server programs. It t1,t3 the start and end of the server program, or some idle time period, so that there is no t1,t3 overhead when the server program processes the client request.
The thread pool not only adjusts the time period generated by the T1,T3, but it also significantly reduces the number of threads created, looking at an example:
Suppose a server handles 50,000 requests a day, and each request requires a separate thread to complete. The number of threads in a thread pool is generally fixed, so the total number of threads does not exceed the number of threads in the thread pool, and the total number of threads is 50000 if the server does not use the thread pool to process these requests. The general thread pool size is far less than 50000. Therefore, server programs that utilize the thread pool do not waste time processing requests in order to create 50000, thereby increasing efficiency.

2. The common thread pool ①newsinglethreadexecutor the thread pool of a single threads, that is, each time only one thread in the threads is working, single-threaded serial execution task ②newfixedthreadexecutor (n) a fixed number of thread pools, Not submitting a task is a thread until the maximum number of thread pools is reached and then goes back into the wait queue until the previous task completes before continuing with the ③newcachethreadexecutor (recommended) cache thread pool, when the thread pool size exceeds the threads required to process the task, a partially idle thread (typically 60 seconds without execution) is reclaimed, and when a task comes, it is smart to add a new thread to execute. ④newschedulethreadexecutor size unlimited thread pool that supports timed and recurring thread execution

The thread pool provided by Java is more powerful, and it is believed that understanding how the thread pool works will not be unfamiliar to the thread pool in the class library.

The role of the thread pool:

The thread pool function is to limit the number of threads executing in the system.
According to the environment of the system, the number of threads can be set automatically or manually to achieve the best performance; less waste of system resources, more caused by the system congestion efficiency is not high. Use the thread pool to control the number of threads and other threads to wait. A task is completed, and then the first task from the queue is executed. If there is no waiting process in the queue, this resource of the thread pool is waiting. When a new task needs to run, it can start running if there are waiting worker threads in the thread pool, otherwise enter the wait queue.

Why use a thread pool:

1. Reduces the number of times a thread is created and destroyed, and each worker thread can be reused to perform multiple tasks.

2. You can adjust the number of threads in the thread pool according to the endurance of the system, prevent the server from being exhausted because it consumes too much memory (approximately 1MB of memory per thread, the more threads open, the more memory is consumed and the last crashes).

The top interface of the thread pool in Java is executor, but strictly speaking, executor is not a thread pool, but a tool for executing threads. The real thread pool interface is executorservice.

Some of the more important classes:

Executorservice

A true thread pool interface.

Scheduledexecutorservice

can be similar to Timer/timertask to solve problems that require repetitive execution of tasks.

Threadpoolexecutor

The default implementation of Executorservice.

Scheduledthreadpoolexecutor

Inheriting Threadpoolexecutor's Scheduledexecutorservice interface implementation, the class implementation of periodic task scheduling.

To configure a thread pool is more complex, especially if the thread pool principle is not very clear, it is likely that the thread pool configured is not superior, so there are some static factories in the executors class that generate some common thread pools.

1. Newsinglethreadexecutor

Creates a single threaded pool of threads. This thread pool has only one thread at work, which is equivalent to single-threaded serial execution of all tasks. If this unique thread ends because of an exception, a new thread will replace it. This thread pool guarantees that the order in which all tasks are executed is performed in the order in which the tasks are submitted.

2.Newfixedthreadpool

Creates a fixed-size thread pool. Each time a task is committed, a thread is created until the thread reaches the maximum size of the threads pool. Once the maximum size of the thread pool is reached, the thread pool will be replenished with a new thread if it ends up executing an exception.

3. Newcachedthreadpool

Creates a cacheable pool of threads. If the size of the thread pool exceeds the thread that is required to process the task,

Then a partially idle (60 second non-performing) thread is reclaimed, and when the number of tasks increases, the thread pool can intelligently add new threads to handle the task. This thread pool does not limit the size of the thread pool, and the thread pool size is entirely dependent on the maximum thread size that the operating system (or JVM) can create.

4.Newscheduledthreadpool

Create a thread pool of unlimited size. This thread pool supports the need to schedule and periodically perform tasks.

Instance

Callable class

 PackageCallableimp;Importjava.util.concurrent.Callable; Public classCallableimpImplementsCallable<integer>{    Private intnum;  PublicCallableimp (intnum) {         This. num=num; } @Override PublicInteger Call ()throwsException {intSum=0;  for(inti=1;i<=num;i++) {sum=sum+i; }        returnsum; }}

Test class

 PackageCallableimp;Importjava.util.concurrent.ExecutionException;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;Importjava.util.concurrent.Future; Public classTest { Public Static voidMain (string[] args)throwsinterruptedexception, executionexception {executorservice es=executors.newfixedthreadpool (2);//New thread poolFuture<integer> F=es.submit (NewCallableimp (100));//ReceiveFuture<integer> F2=es.submit (NewCallableimp (200));//ReceiveSystem.out.println (F.get ());//Get DataSystem.out.println (F2.get ());//Get Data    }}

April 24, 2018 Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.