the code in the critical section (the reason for implicit locking is that you only need to add the synchronized mark to the method or variable, the synchronization mutex Control During thread concurrency will be completed by the system)
Java provides internal support for preventing resource conflicts, namely synchronized.
Note the following points when learning synchronized:
1. If a
When a Java program starts, a thread runs immediately, which is usually called the main thread of the program (main thread) because it is executed at the start of the program. The importance of the main thread is embodied in two aspects:
It is the
thread ends automatically when the method is executed. to manually end a thread, you can use the volatile Boolean variable to exit run () method or cancel the task to interrupt the thread. Click here to view the sample code.13: What if an exception occurs when a thread is running?This is a very tricky
Then, Condition and Condition introduce the main character of this Article. They break down the Object monitor method (wait, policy, and policyall) into completely different objects so that they can be combined with any Lock implementation, multiple waiting sets (wait-set) are provided for each object ). Here, Lock replaces the use of synchronized methods and statements, and Condition replaces the use of Object monitor methods. The following code replaces a previously written
Interrupt Method
Interrupt literally means interruption, but in Java, the thread. Interrupt () method actually notifies the thread in some way and does not directly stop the thread. Write specific tasksCodeUsually we will stop this thread.
If the
Last time, I gave a general overview and summary of Java thread. Of course, most of them are in theory. This time I will explain two common and confusing methods in thread and use the example
Code ...
FDifferences
Sleep () methodSleep () causes the current thread to enter the stuck state (blocking the current
();}}
The thread class run method was found to invoke the testrunnable run (), so the
The code is as follows
Copy Code
New Thread (New testrunnable (i)). Run ()
is not a multithreaded, it executes a thread or is a primary thread.
The
tasks. When another thread stops other jobs, if a thread is always waiting for a quick response to the user's actions, such as clicking the mouse or pressing the key, users will feel better responses from the system.Simulation of simultaneous activities)Even if there is only one processor, the threads in Java seem to run concurrently. The processor runs each
The basics of multithreading and the creation of multithreading are already mentioned in the previous article. But if you want to manage multiple threads well, be sure to have a holistic concept of the thread's life cycle. This section describes the lifetime of the thread, giving you a sense of the state of the threads at various times.
The state process of a thread's lifetimeSuch as:
threads can be from birth to run to death. In the previ
We know that when thread A gives up the execution right for some reason (such as waiting for the completion of IO operations or calling the sleep function), the operating system will schedule another ready state (Runnable) thread B. Only when the event waiting for thread A occurs (such as IO operation completed and sleep time ended) will
This article illustrates the thread hibernation usage of Java thread scheduling. Share to everyone for your reference. The specific analysis is as follows:
Java thread scheduling is the core of Java multithreading, only good sche
First, memory managementTwoThread Exclusive ZoneProgram Counter Register)A program counter is a small amount of memory space, which can be seen as the line number indicator of the bytecode executed by the current thread. In the virtual machine conceptual model, the bytecode interpreter works by changing the value of this counter to select the next byte-code instruction to execute, and the basic functions such as branching, looping, jumping, exception
This article illustrates the thread hibernation usage of Java thread scheduling. Share to everyone for your reference. The specific analysis is as follows:
Java thread scheduling is the core of Java multithreading, only good sche
There are two ways to create a new class that can have a thread running within it. one way is to extend the Thread class. the other is to extend any class and implement the Runnable interface. for the sake of authentication, extending Thread is the simplest approach and is initially used in this book. implementing the Runnable interface tends to work much better
Thread classThe thread class exists at JDK1.0, and multithreaded development that comes into contact with Java basically starts with this class.Thread-Defined threading classThe way to create a thread using thread is simple, thread
Thread class
The thread class exists at JDK1.0, and multithreaded development that comes into contact with Java basically starts with this class.Thread-Defined threading class
The way to create a thread using thread is simple, thread
print 0-1000 threads - } - -}Method 2: take the constructor method to name the thread You first have to add the custom thread class:1 public MyThread () {2 }3 4 public MyThread (String Name) {5 Super (name); 6 }Because this class does not have a parameter construction method, it must be created to use the parameter constructThe test class can call the constructor directly
State transition plots for threads:. Blog temporarily can't upload picturesThread groups for threads :Thread groups: combine multiple threads together.It can classify a batch of threads, and Java allows the program to control the thread group directly.First, create a Runnable implementation class Public class Implements Runnable { publicvoid run () { f
Overview
When you browse a webpage, you can read and scroll the text in the webpage while waiting to load the image. Is this a bad way? In the word processing software, when you open another document to be edited, how can you print a document in the background? You may have enjoyed the benefits of replying to an email and downloading large file attachments. By allowing a multi-threadProgramMultiple tasks at the same time can make these convenient functions possible. This book helps you learn
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.