In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "Java multithreaded synchronized keyword output". In daily operation, I believe many people have doubts about how to output Java multithreaded synchronized keyword. Xiaobian consulted all kinds of information and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "Java multithreaded synchronized keyword output". Next, please follow the editor to study!
1, whether or not to use the difference of synchronized keyword
Public class ThreadTest {public static void main (String [] args) {Example example = new Example (); Thread T1 = new Thread1 (example); Thread T2 = new Thread1 (example); t1.start (); t2.start ();}} class Example {public synchronized void execute () {for (int I = 0; I < 10) + + I) {try {Thread.sleep;} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Hello:" + I);}} class Thread1 extends Thread {private Example example Public Thread1 (Example example) {this.example = example;} @ Override public void run () {example.execute ();}}
Whether or not to add the synchronized keyword before the execute () method, the execution result of this example program will be very different.
Without the synchronized keyword, two threads execute the execute () method at the same time, and the output is two sets of concurrency.
If the synchronized keyword is added, a set of 0 to 9 is output first, and then the next set, indicating that the two threads are executed sequentially.
two。 Multithreading of multiple methods
Change the program and add another method, execute2 (), to the Example class.
Then write a run () method in the thread class Thread2,Thread2 that performs execute2 (). Both methods in the Example class are modified by the synchronized keyword.
Public class ThreadTest {public static void main (String [] args) {Example example = new Example (); Thread T1 = new Thread1 (example); Thread T2 = new Thread2 (example); t1.start (); t2.start ();}} class Example {public synchronized void execute () {for (int I = 0; I < 20) + + I) {try {Thread.sleep ((long) Math.random () * 1000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Hello:" + I) }} public synchronized void execute2 () {for (int I = 0; I < 20; + + I) {try {Thread.sleep ((long) Math.random () * 1000);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("World:" + I);} class Thread1 extends Thread {private Example example; public Thread1 (Example example) {this.example = example;} @ Override public void run () {example.execute ();}} class Thread2 extends Thread {private Example example Public Thread2 (Example example) {this.example = example;} @ Override public void run () {example.execute2 ();}}
If the synchronized keyword is removed, the two methods execute concurrently and do not affect each other.
But as written in the example program, even if there are two methods:
The result of execution is always to execute the output of one thread and then execute another thread.
Description:
If an object has multiple synchronized methods and a thread has entered a synchronized method at some point, no other thread can access any synchronized methods of the object until the method has been executed.
Conclusion:
When the synchronized keyword modifies a method, the method is called a synchronous method.
Every object in Java has a lock, or monitor. When a thread accesses an object's synchronized method, it locks the object so that no other thread can access the object's synchronized method (in this case, all synchronous methods, not just the same method) until the previous thread finishes executing the method (or throws an exception). Only when the lock on the object is released will it be possible for other threads to access the object's synchronized method.
Note that the object is locked at this time. If it is a different object, there is no restrictive relationship between the objects.
When you try to construct a second thread object in your code, pass in a new Example object, and there is no constraint between the execution of the two threads.
3. Consider static synchronization method
When a method modified by the synchronized keyword is also modified by static, as mentioned earlier, a non-static synchronization method locks the object, but the static method does not belong to the object, but belongs to the class. It locks the Class object of the class in which the method is located.
No matter how many objects a class generates, they correspond to the same Class object.
Public class ThreadTest {public static void main (String [] args) {Example example = new Example (); Thread T1 = new Thread1 (example); / even if different objects are passed here, static method synchronization still does not allow multiple threads to execute example = new Example (); Thread T2 = new Thread2 (example); t1.start (); t2.start () }} class Example {public synchronized static void execute () {for (int I = 0; I < 20; + + I) {try {Thread.sleep ((long) Math.random () * 1000);} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("Hello:" + I);}} public synchronized static void execute2 () {for (int I = 0; I < 20; + + I) {try {Thread.sleep ((long) Math.random () * 1000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("World:" + I);} class Thread1 extends Thread {private Example example; public Thread1 (Example example) {this.example = example } @ Override public void run () {Example.execute ()}} class Thread2 extends Thread {private Example example; public Thread2 (Example example) {this.example = example;} @ Override public void run () {Example.execute2 ();}}
So in the case of static methods (execute () and execute2 () both add the static keyword), even if different Example objects are passed to two threads, the two threads still restrict each other and must execute one before the next.
Conclusion:
If a synchronized method is static, when a thread accesses the method, it locks not the object of the synchronized method, but the Class object of the class of the synchronized method. In Java, no matter how many objects a class has, these objects will correspond to a Class object, so when a thread accesses two static,synchronized methods of two objects of the same class respectively, their execution order is also sequential, that is, one thread executes the method first, and the other thread starts after the execution is completed.
4. Synchronized block
Synchronized block writing:
Synchronized (object)
{
}
Indicates that the thread locks the object object when it executes. Note that this object can be an object of any class, or you can use the this keyword.
In this way, you can specify the locked object on your own.
Public class ThreadTest {public static void main (String [] args) {Example example = new Example (); Thread T1 = new Thread1 (example); Thread T2 = new Thread2 (example); t1.start (); t2.start ();} class Example {private Object object = new Object () Public void execute () {synchronized (object) {for (int I = 0; I < 20; + + I) {try {Thread.sleep ((long) Math.random () * 1000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("Hello:" + I) } public void execute2 () {synchronized (object) {for (int I = 0; I < 20; + + I) {try {Thread.sleep ((long) Math.random () * 1000) } catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("World:" + I);}} class Thread1 extends Thread {private Example example Public Thread1 (Example example) {this.example = example;} @ Override public void run () {example.execute ();}} class Thread2 extends Thread {private Example example; public Thread2 (Example example) {this.example = example;} @ Override public void run () {example.execute2 ();}
The effect of example program 4 is the same as that of example program 2, which makes two threads execute sequentially rather than concurrently. When one thread executes, the object object is locked and the other thread cannot execute the corresponding block.
The synchronized method is effectively equivalent to enclosing all statements in the method with a synchronized block, and then passing the this keyword in parentheses in the synchronized block. Of course, if it is a static method, it is the class object that needs to be locked.
Maybe there are only a few lines of code in a method that involve thread synchronization, so the synchronized block controls access to multiple threads more finely than the synchronized method, only the content in the synchronized block cannot be accessed by multiple threads at the same time, and other statements in the method can still be accessed by multiple threads at the same time (including before and after the synchronized block).
Note: data protected by synchronized should be private.
At this point, the study on "how to output Java multithreaded synchronized keywords" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.