have been written by another thread, 2) write the next variable that might be read by another thread(2) Consistency synchronization : When you modify multiple related values, you want the other threads to see this set of changes atomically-either see all the changes or see nothing.This applies to related data items, such as the location and rate of particles, and metadata items such as the data values cont
program will have some uncertain bugs. This kind of bugs is hard to find and will repeat because of random competition between threads. An example is unordered processing. For details, see the answer.
13) how to stop a thread in Java?Java provides a wide range of APIS, but does not provide APIs for stopping threads. JDK 1.0 has some control methods like stop (),
JAVA concurrent programming 9_correct thread closure, java concurrent thread
Java does not provide any security mechanisms to terminate threads. Although methods such as Thread. stop and suspend provide such mechanisms, they have
After an External Thread calls the thread. Interrupt () method to a thread, the Java language processing mechanism is as follows:If the thread is in a resumable state. wait (), or selector. select (),
Brief Introduction:
Using the join and interrupt functions in Java multithreading
"Java Programming thought" P669 ~ P670
A thread can invoke the join () method on another thread, and the effect is to wait for a period of time until the second
This is an example from the Java programming mindset[Java]View Plaincopy
Package demo.thread;
/**
*sleep () is a static method that belongs to a class that is used to block the current thread
*join () is a thread that synchronizes threads, such as calling T.join () in a
execution) within the specified number of milliseconds, which is affected by the system timer and scheduler precision and accuracy.
Because the sleep () method is a method of the thread class, it cannot alter the object's machine lock. So when you call sleep () in a synchronized method, the thread is dormant, but the object's machine lock is not freed, and the object is still inaccessible to other thread
One: See demo directlyOutput Result: -------Tue Feb 19:57:02 CST-----------Tue Feb 19:57:03 CST-----------Tue Feb 19:57:04 CST-----------Tue Feb 19:57:05 CST-----------Tue Feb 19:57:06 CST-----------Tue Feb 19:57:07 CST-----------Tue Feb 19:57:08 CST-----------Tue Feb 19:57:09 CST-----------Tue Feb 19:57:10 CST-----------Tue Feb 19:57:11 CST----It can be seen that, after 10 seconds, the thread terminates, Thread.Interrupt (), indicating that the
exception.private void fileprocess (file file) throws interruptedexception{if (File.getname (). Equals (FileName)) {System.out.printf ("%s:%s\n", Thread.CurrentThread (). GetName (), File.getabsolutepath ());}if (thread.interrupted ()) {throw new Interruptedexception ();}}6. Write a main class and write a method that contains the main () method.public class Main {public static void Main (string[] args) {}7. Create an object of the FileSearch class, use it as an incoming parameter to create a
object.third, the life cycle of thread1. Start threadClass ThreadClass extends Thread {@Overridepublic void Run () {for (int i = 0; i Calling the Start method of the thread subclass runs the Run method. From log can be seen in two threads. But threads are all linear from the go-back run.public void Run ()Assuming that the thread is constructed using an independe
One.In conjunction with the previous section, do not assume that the marked thread can be stopped, there is still a situation to stop.The whole function is to add the wait () method, which causes Try-catch to join.The DOS results show that the program did not stop, and the main thread ended. The remaining two threads are not finished and are in a waiting state.When these two threads get the lock in, wait st
One, the thread state of the check
In general, you cannot determine the running state of a thread, and you can use the IsAlive () method to determine whether a thread is still active for those threads that are in an unknown state. Of course even an active thread does not mean that the
( isInterrupted( ClearInterrupted);
), The interrupt state of the latter thread is not affected.
Ii. Example.
Main main = Thread t = System.out.println("mainmainmain" Thread.sleep(2000 } Runnable runnable = i = 0
In the previous blog post, the use of the Member method interrupt () was introduced, and the last part of the article went on to describe the remaining two methods related to interrupts.2. Member method new Thread (). isinterrupted ()It is common to use the Thread.CurrentThread (). isinterrupted () method to determine whether a thread has been sent an
Several ways to interrupt Java iterationsThis article summarizes the usage of several keywords in Java that interrupt iterations, and return does not say a word about break and continue and how to implement the GOTO keyword in other languages in Java.
The difference
Java multi-thread Development Series 4: Playing with multiple threads (thread control 2), java multi-thread
In the thread control section above (click here for details), we have explained the
Java multi-thread 3 thread collaboration and communication instance, java multi-thread
The difficulty of Multithreading is mainly the multi-thread communication and collaboration. The common Synchronization Methods mentioned in th
thread State (five states)The life cycle of a Java thread consists of 创建,就绪,运行,阻塞,死亡 5 states. A Java thread is always in one of these 5 lifecycle States and can be converted between different states under certain conditions.Create state (New
Related Articles Directory:java thread pool threadpoolexecutor usage and analysis (i)java thread pool threadpoolexecutor usage and Analysis (ii)-execute () principlejava thread pool threadpoolexecutor usage and Analysis (iii)-Termination thread pool principleThe following is
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.