Common methods of multithreading

1、sleep()

Suspends the current thread (that is, the thread calling the method) for a period of time, giving other threads a chance to continue execution, but it does not release the object lock. That is to say, if there is synchronized synchronization, other threads still cannot access shared data. Note that this method catches exceptions.

For example, if there are two threads executing at the same time (without synchronization), one thread has a priority of MAX_PRIORITY and the other is MIN_PRIORITY. If there is no Sleep() method, only after the high-priority thread is executed, the low-priority thread can execute; but After the high-priority thread sleep(500), the low-priority thread has a chance to execute.

In short, sleep() can give low-priority threads a chance to execute, and of course, threads of the same priority and high-priority have a chance to execute.


2、join()

The join() method makes the thread calling this method finish executing before that, that is, wait for the thread of this method to finish executing before continuing to execute. Note that this method also needs to catch exceptions.

The effect exists only between the thread executing the join method and the thread that called it

If t2.join() is called in the t1 thread, t1 can continue to execute after the t2 thread is executed.

3、yield()

This method is similar to sleep(), except that the user cannot specify how long to pause, and the yield() method can only allow threads of the same priority to have a chance to execute.

4、wait()和notify()、notifyAll()

These three methods are used to coordinate access to shared data by multiple threads, so they must be used within the synchronized block. The synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is very inflexible. How can other threads have the opportunity to access the shared data when the current thread has not exited the synchronized data block Woolen cloth? At this time, use these three methods to flexibly control.

The wait() method causes the current thread to suspend execution and release the object lock flag, so that other threads can enter the synchronized data block, and the current thread is put into the object waiting pool. When the notify() method is called, an arbitrary thread will be removed from the object's waiting pool and placed in the lock flag waiting pool. Only threads in the lock flag waiting pool can acquire the lock flag; if there are no threads in the lock flag waiting pool , notify() does not work.

notifyAll() removes all threads waiting for that object from the object waiting pool and puts them in the lock flag waiting pool.

Note that these three methods are all methods of java.lang.Object.



2. run and start()

Put the code that needs to be processed into the run() method, and the start() method to start the thread will automatically call the run() method, which is specified by the java memory mechanism. And the run() method must have public access, and the return value type is void.


3. The keyword synchronized

This keyword is used to protect shared data, of course, the premise is to distinguish which data is shared data. Each object has a lock flag. When a thread accesses the object, the data modified by Synchronized will be "locked", preventing other threads from accessing. After the current thread has finished accessing this part of the data, the lock flag is released, and other threads can access it.



Fourth, wait() and notify(), notifyAll() are methods of the Object class, and sleep() and yield() are methods of the Thread class.

(1), the commonly used wait methods are wait() and wait(long timeout);

void wait() Causes the current thread to wait before other threads call the notify() method or notifyAll() method of this object.

void wait(long timeout) Causes the current thread to wait until another thread calls the notify() method or notifyAll() method of this object, or exceeds the specified amount of time.

After wait(), the thread will release the "lock flag" it holds, so that other shnchronized data in the object where the thread is located can be used by other threads.


Because wait()h and notify() operate on the object's "lock flag", they must be called in a Synchronized function or a synchronized block. If the call is made in a non-synchronized function or a non-synchronized block, although it can be compiled and passed, an IllegalMonitorStateException exception will occur at runtime. .


(2), Thread.sleep (long millis) must have a time parameter.

sleep(long) causes the current thread to enter a stagnant state, so the thread executing sleep() will definitely not be executed within the specified time;

sleep(long) can give low priority threads a chance to execute, and of course threads with the same priority have a chance to execute;

sleep(long) will not release the lock flag.


(3), yield() has no parameters

The sleep method makes the currently running thread sleep for a period of time and enters a non-operational state. The length of this period of time is set by the program. The yield method makes the current thread give up the CPU ownership, but the given time cannot be set. of.

yield() also does not release the lock flag.

In fact, the yield() method corresponds to the following operations; first check whether there are currently threads of the same priority in the same runnable state, if so, give the CPU ownership to the secondary thread, otherwise continue to run the original thread, so The yield() method is called "yielding", which gives the opportunity to run to other threads of the same level.


The sleep method allows lower-priority threads to get a chance to run, but when the yield() method is executed, the current thread is still in a runnable state, so it is impossible to give the lower-priority thread the opportunity to obtain CPU ownership at this time. In a running system, if the higher-priority thread does not call the sleep method and is not blocked by I/O, then the lower-priority thread can only wait for all higher-priority threads to finish before they have a chance to run .


yield() only makes the current thread return to the executable state. All threads that execute yield() may be executed immediately after entering the executable state, so the yield() method can only make threads of the same priority execute. Opportunity.

1、sleep()

Suspends the current thread (that is, the thread calling the method) for a period of time, giving other threads a chance to continue execution, but it does not release the object lock. That is to say, if there is synchronized synchronization, other threads still cannot access shared data. Note that this method catches exceptions.

For example, if there are two threads executing at the same time (without synchronization), one thread has a priority of MAX_PRIORITY and the other is MIN_PRIORITY. If there is no Sleep() method, only after the high-priority thread is executed, the low-priority thread can execute; but After the high-priority thread sleep(500), the low-priority thread has a chance to execute.

In short, sleep() can give low-priority threads a chance to execute, and of course, threads of the same priority and high-priority have a chance to execute.


2、join()

The join() method makes the thread calling this method finish executing before that, that is, wait for the thread of this method to finish executing before continuing to execute. Note that this method also needs to catch exceptions.

The effect exists only between the thread executing the join method and the thread that called it

If t2.join() is called in the t1 thread, t1 can continue to execute after the t2 thread is executed.

3、yield()

This method is similar to sleep(), except that the user cannot specify how long to pause, and the yield() method can only allow threads of the same priority to have a chance to execute.

4、wait()和notify()、notifyAll()

These three methods are used to coordinate access to shared data by multiple threads, so they must be used within the synchronized block. The synchronized keyword is used to protect shared data and prevent other threads from accessing the shared data, but the process of the program is very inflexible. How can other threads have the opportunity to access the shared data when the current thread has not exited the synchronized data block Woolen cloth? At this time, use these three methods to flexibly control.

The wait() method causes the current thread to suspend execution and release the object lock flag, so that other threads can enter the synchronized data block, and the current thread is put into the object waiting pool. When the notify() method is called, an arbitrary thread will be removed from the object's waiting pool and placed in the lock flag waiting pool. Only threads in the lock flag waiting pool can acquire the lock flag; if there are no threads in the lock flag waiting pool , notify() does not work.

notifyAll() removes all threads waiting for that object from the object waiting pool and puts them in the lock flag waiting pool.

Note that these three methods are all methods of java.lang.Object.



2. run and start()

Put the code that needs to be processed into the run() method, and the start() method to start the thread will automatically call the run() method, which is specified by the java memory mechanism. And the run() method must have public access, and the return value type is void.


3. The keyword synchronized

This keyword is used to protect shared data, of course, the premise is to distinguish which data is shared data. Each object has a lock flag. When a thread accesses the object, the data modified by Synchronized will be "locked", preventing other threads from accessing. After the current thread has finished accessing this part of the data, the lock flag is released, and other threads can access it.



Fourth, wait() and notify(), notifyAll() are methods of the Object class, and sleep() and yield() are methods of the Thread class.

(1), the commonly used wait methods are wait() and wait(long timeout);

void wait() Causes the current thread to wait before other threads call the notify() method or notifyAll() method of this object.

void wait(long timeout) Causes the current thread to wait until another thread calls the notify() method or notifyAll() method of this object, or exceeds the specified amount of time.

After wait(), the thread will release the "lock flag" it holds, so that other shnchronized data in the object where the thread is located can be used by other threads.


Because wait()h and notify() operate on the object's "lock flag", they must be called in a Synchronized function or a synchronized block. If the call is made in a non-synchronized function or a non-synchronized block, although it can be compiled and passed, an IllegalMonitorStateException exception will occur at runtime. .


(2), Thread.sleep (long millis) must have a time parameter.

sleep(long) causes the current thread to enter a stagnant state, so the thread executing sleep() will definitely not be executed within the specified time;

sleep(long) can give low priority threads a chance to execute, and of course threads with the same priority have a chance to execute;

sleep(long) will not release the lock flag.


(3), yield() has no parameters

The sleep method makes the currently running thread sleep for a period of time and enters a non-operational state. The length of this period of time is set by the program. The yield method makes the current thread give up the CPU ownership, but the given time cannot be set. of.

yield() also does not release the lock flag.

In fact, the yield() method corresponds to the following operations; first check whether there are currently threads of the same priority in the same runnable state, if so, give the CPU ownership to the secondary thread, otherwise continue to run the original thread, so The yield() method is called "yielding", which gives the opportunity to run to other threads of the same level.


The sleep method allows lower-priority threads to get a chance to run, but when the yield() method is executed, the current thread is still in a runnable state, so it is impossible to give the lower-priority thread the opportunity to obtain CPU ownership at this time. In a running system, if the higher-priority thread does not call the sleep method and is not blocked by I/O, then the lower-priority thread can only wait for all higher-priority threads to finish before they have a chance to run .


yield() only makes the current thread return to the executable state. All threads that execute yield() may be executed immediately after entering the executable state, so the yield() method can only make threads of the same priority execute. Opportunity.

Guess you like

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