"Java Multithreaded Programming Core Technology" - Notes

  As a business developer, there are not many technologies that you can use in your work. Although what I always say, multi-threading, concurrency, injection, attack! But in practice, these things are not necessarily useful. Because, the framework we use already does these things.

  For example, in web development, there are a large number of requests coming in from outside. It stands to reason that we should consider concurrency issues. But in fact, after spring receives the request and assigns it to the controller, it is already thread-safe, so what we have to do is to ensure thread safety from the beginning of the controller to the end of the request and response.

  Multithreading seems to have a lot of things to pay attention to. After reading "Java Multithreaded Programming Core Technology", make a summary. Overall, there are actually not so many things, not so complicated.

1. Multithreading Basics

  Java's multi-threading is reflected in the Thread class and the Runable interface! Sharing data, there is a thread safety problem, not sharing data, there is no thread safety. Many methods of stopping threads in java have been abolished, and it is not recommended to use methods such as resume, stop, and suspend. Setting thread priority (setPriority) may improve the speed of thread execution. The existence of a daemon thread requires at least one non-daemon thread to be running, which means that a daemon thread cannot exist independently, and its ability is relatively low. For example, GC is a daemon thread. When your code is executing, the GC is running and can be performed at any time. Memory recycling, when your program is completed, the GC thread does not exist.

2. Concurrent access to objects and variables

  synchronized synchronization method, lock object, lock code block, lock method, lock variable,

  volatile keyword, use volatile thread to solve the problem of synchronous infinite loop without stopping. volatile forces the value of the variable to be obtained from the common heap, keeping consistency.

 3. Inter-thread communication

  wait/notify is the most basic way to implement communication between threads. It is very convenient to implement the consumer/producer pattern.

  After the wait method, the lock is released immediately, and the notify lock is not released. That is, after the wait method is executed, subsequent concurrent requests can enter the block, and notify needs to wait until the synchronized code block is executed before releasing the lock.

  notifyAll() wakes up all waiting threads.

  The join() method releases the lock and waits for the thread to finish executing. Thread.sleep() does not release the lock waiting.

  Communicate through pipes, passing as a stream of characters. PipeWriter, PipeReader, outputSream.connect(inputStream) associates the input stream with the output.

  ThreadLocal can be considered as a thread-level global variable, that is, in this thread, the value can be obtained anywhere without worrying about thread safety. set(), get() methods are used to set and access.

4. Use of Lock

  ReentrantLock,

5. timer

  timer.schedule(task, dateRef) is executed once, and timer.schedule(task, dateRef, period) is executed periodically.
6. Singleton Pattern and Multithreading

  if(obj == null) {
    synchronized(MyObject.class) {
      if(obj == null) {
        obj = new MyObject();
      }
    }
  }

  Well, it seems that the knowledge of multi-threading is still very little. Don't have too much, just be fine!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325170579&siteId=291194637