Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Java thread synchronization is called in different threads

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you how Java thread synchronization is called in different threads, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

Java thread synchronization requires all of us to keep learning, but we still need to pay attention to some important code when learning. Let's take a detailed look at Synchronized, which, as the name implies, represents Java thread synchronization. So, how do you use it in java programming?

Let's first look at a situation where we call synDemo.synMethord1 () and synDemo.synMethord2 () in two different threads for the same variable synDemo.

Package com.cnblogs.gpcuster;/** @ author Aaron.

Guo * * /

Public class Tester {public static void main (String []

Args) {final SynDemo synDemo = new SynDemo (); Thread

Thread1 = new Thread () {@ Override public void run () {

/ / TODO Auto-generated method stub

Super.run (); synDemo.synMethord1 ();}}; Thread thread2

= new Thread () {@ Override public void run () {/ / TODO

Auto-generated method stub

Super.run (); synDemo.synMethord2 ();}}; thread1.start ()

Thread2.start (); while (true) {try {Thread.sleep (1000)

System.out.println ("main");} catch (InterruptedException e)

{/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

}

}

Our SynDemo is defined as this:

Package com.cnblogs.gpcuster;/** @ author

Aaron.Guo * * /

Public class SynDemo {public void synMethord1 () {

While (true) {try {Thread.sleep (1000); System.out.

Println ("synMethord1");} catch (InterruptedException e)

{/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

}

Public void synMethord2 () {while (true) {try {Thread.

Sleep (1000); System.out.println ("synMethord2");} catch

(InterruptedException e) {/ / TODO Auto-generated catch block

E.printStackTrace ()

}

}

}

}

The above is how Java thread synchronization is called in different threads. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report