There are several states of the Java thread:
1: Create state (new): Thread is new and does not call start
2: Ready state (Runnable): Also known as an executable state, the thread scheduler has not allocated CPU time slice execution to the thread when it is in a ready state when the thread's Start method is invoked.
3: Running state (Running): The thread scheduler allocates CPU time slices to execute thread code.
4: Blocking state (Blocked): The threa
Source: http://blog.csdn.net/fanaticism1/article/details/9966163#1, the lock provides two main features: Mutual exclusion (mutual exclusion) and visibility (visibility). Mutual exclusion allows only one thread to hold a particular lock at a time, so you can use this attribute to implement a coordinated access protocol to shared data so that only one thread can use that shared data at a time. Visibility is more complex, and it must ensure that the changes made to the shared data before the lock i
First of all explain: Method lock and Object lock is said to be a thing, that is, only method lock or object lock and class lock two kinds of locks
The author uses the easy language to explain the complex technology so thoroughly, learn, spread.
Original address: http://zhh9106.iteye.com/blog/2151791
In Java programming, often need to use synchronization, and the most used is probably the Synchronized keyw
, essentially adding aAcc_synchronized (Access SYNCHRONIZED) flag, when the method is called, first checks whether the method's acc_synchronized is set, and if set, the execution thread will first get the object's monitor lock, The method body can be executed after the method has been successfully executed before releasing monitor. No other thread can get the monitor lock on the object during the execution of the method.The
In Java multithreaded programming, the code that needs to execute concurrently is placed inside the run method of the thread class, and then the object of the thread class is created, and the start () method is called to start execution.When a piece of code needs to be mutually exclusive, it can be decorated with the Synchronized keyword, which discusses how the Synchro
Java Thread Series (v) synchronizedsynchronized lock re-entry
The keyword synchronized has the ability to re-enter the lock, that is, when using synchronized, when the thread waits for the lock on an object, it can again get the lock on the object when it is requested again. Release the lock when an exception occurs.
S
In the "Java" thread concurrency, mutual exclusion and synchronization (click Open link) in the operating system through the operation of the original method of semaphore control, complete the thread of mutual exclusion and synchronization, say a digression, in fact, this signal volume algorithm, is the famous Dijkstra created, that is, the data structure, The shortest path algorithm, Dijkstra algorithm and Dijkstra algorithm are the contributors of t
read-write cache,Therefore, we do not necessarily follow the inherent logic when loading and executing the code. only data dependencies prohibit command reordering. As-if-serialRegardless of reordering, the results of the execution of the program and the sequential execution of the code are consistent (Java compilers, runtimes, and processors will ensure that Java programs follow as-if-serial semantics on
Volatile: Only guarantee the visibility of shared resources, any changes are written in main memory, all threads can be seen immediately, for the new value does not depend on the old value of the case.Synchronized: Ensures operational atomicity consistency and visibility.The difference between volatile and synchronized: Volatile only causes variables to be known to other threads as soon as the value changes, and does not guarantee the sequential exec
First, Java Concurrency FoundationWhen an object or variable can be shared by multiple threads, it is possible to cause problems with the logic of the program. There is a variable in an object i=0, there are two threads, a, a, I plus 1, this time there is a problem to appear, the key is that I plus 1 of the process is not atomic operation. To increment the I, the first step is to get the value of I, when a gets the value of I is 0, before a new value
The lock implementation provides a wider range of locking operations than is possible with synchronized methods and statements.This implementation allows for a more flexible structure that can have very different properties and can support multiple related Condition objects.Use producers and consumers as an example!!!Import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import j
This topic is proposed is the original thread to solve the idea of concurrency. Currently resolving thread concurrency, which can be a lock interface combined with condition concurrency problem has always been a thread essential topic.Java is the first mainstream programming language with built-in support for multithreading. Before Java5, the support of multithreading is mainly implemented by synchronizing the block structure (synchronized with Wait,n
Or an example of a previous ticket sale:Class Test implements runnable{ private int num =; Object obj = new Object (); public void Run () { while (true) { if (num >= 0) { try { thread.sleep]; } catch (Exception e) { //Todo:handle exception System.out.println (E.tostring ());
("" +num);
System.out.println ("" +num);}}}}
An example: A bank can save money, two people save at the same time, each save 3 times, each save 100
public class savemoney{public
static void Main (string[] args) {person person
= new person ();
thread T1 = new thread (person);
Thread t2 = new Thread (person);
thread t3 = new thread (person);
T1.start ();
T2.start ();
T3.start ();
}
Class Person implements runnable{
Bank
1.synchronized the basis for synchronizationEach object in Java can be used as a lock, with 3 different manifestations.1. For the normal synchronization method, the lock is the current instance object.2. For the static synchronization method, the lock is the class object of the current classes.3. For a synchronous method block, the lock is a configuration object inside the synchonized bracket.When a thread
=NewThread (NewTest (), "Thread_one"); Thread Thread_two=NewThread (NewTest (), "Thread_two"); Thread_one.start (); Thread_two.start ();//no blockage occurred//Thread_two 2//Thread_one 1//Thread_two 3//Thread_one 4//Thread_two 5//Thread_one 6//Thread_two 7//Thread_one 8//Thread_two 9//Thread_one Ten
If the bracket is a class, the scope is an object, and the effect is the same as the synchronized modifier static methodPublic [Static] void runtest ()
This article is a summary of the study of the article on the network, thank you for your selfless sharing.When you use the Synchronized keyword to protect a block of code, you must pass a reference to an object as a parameter. Typically, you will use the This keyword to refer to the object that executes the method, but you can also use a different object reference. Typically, these objects are created only for this purpose. For
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.