Discover java thread shared variable example, include the articles, news, trends, analysis and practical advice about java thread shared variable example on alibabacloud.com
Volatile in Java does not guarantee thread safety (example explained)Reprinted 2017-09-04 Think-007 I want to commentThe following small series to bring you a piece of Java in the volatile can not guarantee thread safety (the example
A multithreaded Java program, until all threads have finished executing, the entire program will not exit. (Note that all non-background threads (Non-daemon thread) are executed, and if a thread executes the System.exit () method, the program exits. Sometimes, you want to abort a thread's execution, such as if you want to quit the program, or you want to cancel a
======= subclasses are instantiated by default when the parent class is empty, the constructor is super (), which can be omitted.However, when the parent class does not have an empty construction method, it must call this method in the subclass to instantiate the parent class before the instance class is real.The default constructor for a parent class that is empty is automatically called by default in the subclass.======== when a parent class refers to a subclass object, it calls different clas
has been loaded:%s\n",
New Date ());
10. Run the program to see the execution effect.
Know the reason why
When we run this sample program, we can see two threads starting their execution. First, the datasourcesloader completes its execution, and then the networkconnectionsloader completes its execution. At this point, the main thread continues its execution, and then prints out the termination information to the console.
Endless
system.
Key Points of thread sleep
1. The current thread is always suspended when the thread sleep
2. Before being awakened and executed, the actual time for thread sleep depends on the system timer and scheduler. For idle systems, the actual sleep time is very close to the specified sleep time, but for busy systems,
An example of understanding wait () and Policy ()
The following is a post I saw on the csdn forum, which involves the understanding of synchronization, wait (), notify (), and other concepts, I tried to analyze the wait () and Policy () methods based on some original replies and related concepts in think in Java. Thank you for your advice.The problem is as follows:// Analyze this program and explain it, foc
, it encounters a circle (as shown), in other words, it is a loop, it has to loop several times. Once it's turned up, it can't be executed. This means that the following code is not running and we want to run it. Don't want to because this cycle in here, delay time, how to do?At this point, it is possible to have a separate execution path. To be responsible for the execution of this piece of content.The left is the main thread, the right side is the
Beginning with Java 1.5, it provides a thread pool for everyone to use. The function is quite complete. The following is a simple example. Complex examples will be put in the following blogs.
Import java. util. List;Import java. util. concurrent. ExecutorService;Import
Java 1.5 starts, provides the thread pool for everybody to use, the function is quite complete. The following is a simple example. The complex example will be placed in a few blogs later.
Import java.util.List; Import Java.util.concurrent.ExecutorService; Import java.util.concurrent.Executors; Import Java.util.concurr
Copy Code code as follows:
Package Com.yao;
Import Java.util.concurrent.ExecutorService;Import java.util.concurrent.Executors;Import Java.util.concurrent.Semaphore;
/*** Java 5.0 adds 4 synchronization devices that coordinate the process between threads, respectively:* Semaphore, Countdownlatch, Cyclicbarrier and exchanger.* This example mainly introduces semaphore.* Semaphore is a tool used to m
ImportJava.util.concurrent.CountDownLatch;ImportJava.util.concurrent.ExecutorService;Importjava.util.concurrent.Executors;/*** Simulation of the 100-meter race, 10 players are ready, only waiting for the referee commanded. When everyone gets to the finish line, the game is over. * @authorLiuchao **/ Public classActor { Public Static voidMain (string[] args)throwsinterruptedexception {//10 Athletes FinalCountdownlatch count =NewCountdownlatch (10); //t
For example, there are three threads. After 1 is executed, specify 2 for execution, 3 for execution after 2 for execution, and 1 for execution after 3 for execution.
Public class threeconditioncommunication {
Public static void main (string [] ARGs ){Final business = new business ();New thread (New runnable (){@ OverridePublic void run (){For (INT I = 1; I Business. sub2 (I );}}}). Start ();New
For example, there are three threads. After 1 is executed, specify 2 for execution, 3 for execution after 2 for execution, and 1 for execution after 3 for execution.
Public class ThreeConditionCommunication {
Public static void main (String [] args ){
Final Business business = new Business ();New Thread (New Runnable (){
@ OverridePublic void run (){
For (int I = 1; I Business. sub2 (I );}
}}). Start ();
Ne
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.