"Graphical multi-threaded Java design patterns" notes Prologue

  1. Thread started in two ways:
  • Start thread usage instance of a subclass of Thread;
public class PrintThread extends Thread{
	
	private String message;
	
	public PrintThread(String message){
		this.message = message;
	}
	
	@Override
	public void run() {
		int index = 0;
		while(index<1000){
			System.out.println(message);
			index++;
		}
	}

	public static void main(String[] args) {
		new PrintThread("apple").start();
		new PrintThread("orange").start();
	}
}
  • Examples implement Runnable, as new Thread () constructor parameter;
public class Printer implements Runnable{

	private String message;
	
	public Printer(String message){
		this.message = message;
	}
	
	@Override
	public void run() {
		int index = 0;
		while(index<1000){
			System.out.println(message);
			index++;
		}
	}
	
	public static void main(String[] args) {
		new Thread(new Printer("apple")).start();
		new Thread(new Printer("orange")).start();
	}
}
  1. Start calling thread Thread instance the start () method, rather than run () method;

  2. If the CPU is only one, then the order of the concurrent processing is performed (each time division switching threads occupied CPU, so the net effect is a serial process), and if there are a plurality of CPU, concurrent processing may then be run in parallel;

  3. Multi-threaded programming, according to mission requirements must write the correct thread mutex processing and synchronization ;

  4. Program during normal operation (except forcibly stop exit), until after the termination of all the threads, the program will terminate.

  5. java.util.concurrent package defines an abstract factory creates threads interfaces ThreadFactory, hides the new Thread code.
    Executors.defaultThreadFactory () Returns ThreadFactory interface instance.

		ThreadFactory threadFactory = Executors.defaultThreadFactory();
		threadFactory.newThread(new Printer("apple")).start();
  1. Pause threads: the thread code Thread.sleep (1000), Thread static methods.

  2. Data Race: simultaneous operation of multiple threads in a data or resources due to the anticipation of a condition called data race or race conditions.

  3. java keyword synchronized to perform exclusive processing threads.
    1) synchronized :( object lock method, as an example of the lock, i.e. this)

	public synchronized void deposit(int m){
		//取款
	}

Here Insert Picture Description
Note: The figure of two synchronized methods share a lock!
Each instance has a separate lock. Therefore, not to say that one instance of synchronized methods are being implemented, while other instances synchronized method will not be able to run.
2) synchronized code block:

		synchronized(this){
			//业务代码
		}
  1. Locks, all instances of the implementation of this class are mutually exclusive;
    the synchronized static methods:
public static synchronized void deposit(int m)

synchronized code block:

		synchronized(Bank.class){
			//业务代码
		}
  1. Thread collaboration scenario:
    the normal queue (queue non thread-safe), a plurality of read queue thread, when there is no data in the queue, the read thread wait ; write a plurality of threads, the thread when the write data is written, must notify the waiting thread can read the data take.

  2. java thread can be achieved through cooperation wait, notify, notifyAll.
    wait wait approach is to let the thread, and notify and notifyAll wake up waiting threads.
    Conditions of these three methods can be implemented is a, in the thread. Second, the thread to acquire the lock.
    These three methods are methods of the Object class. It is also a method of Thread.

  3. ---- lounge waiting queue thread. All instances have a wait queue, the queue after it is stored thread calls the wait method.

  4. When any of the following occurs, the thread exits back to the waiting queue:

    1. There are other threads notify method to wake up the thread.
    2. There notifyAll method other threads to wake up threads.
    3. There are other methods interrupt threads to wake up threads.
    4. wait timeouts.
  5. Execution wait, this.wait () is equal to wait ().

  6. To perform the wait method, the thread must hold the lock (which is the rule), but if the thread into the wait queue, the lock will release its instances.

  7. notify () will wake up waiting in the queue a thread exit queue. Thread want to enter a wait of operation, also need to re-acquire the lock . Not wake up, then you can wait to the next operation, it is necessary to reacquire instance locks.

  8. In general, the code when using notifyAll () than using code (when) notify to be robust. Because notify may have missed awakened thread, causing the thread has been in the lounge (WAITING) state.

  9. Thread state migration, the thread has the following state:
    1) NEW create the initial state of the thread;
    2) RUNNABLE state of the thread is running;
    3) TERMINATED thread termination status.
    4) WAITING, TIMED_WAITING after a wait () method.
    5) BLOCKED blocked state (state competition to obtain a lock, the lock before won);
    acquiring thread state can call the Thread class getState method.

  10. BLOCKED and WAITING, TIMED_WAITING the difference:
    the Java documentation official definition ----
    BLOCKED state: "The state refers to a thread blocked waiting for monitor lock"
    the WAITING state is: "a thread is waiting for another thread to perform an action when in this state "
    TIMED_WAITING status as:" a thread wait for another thread to complete an action would be in this state "within a specific wait time
    Thread.sleep (sleep time) and wait (timeout) will enter TIMED_WAITING state.

Published an original article · won praise 0 · Views 167

Guess you like

Origin blog.csdn.net/gs4214101/article/details/104782178