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

What is the relationship between Java threads and Objec

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

Share

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

What is the relationship between Java threads and Objec? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Java thread in the continuous development, in the process of development, we need to constantly learn the relevant code knowledge. Let's take a detailed look at how to learn the relevant technical information. Before creating a startup Java thread, create an Object object that competes for use between threads, and then pass a reference to the Object object to the lock member variable of each thread object.

In this way, the lock member of each thread points to the same Object object. In the run method, we use synchronzied blocks to partially block the lock object, which allows the Java thread to compete for the shared object lock, thus achieving synchronization.

Code

Package com.vista; class MyThread implements java.lang.Runnable {private int threadId; private Object lock; public MyThread (int id, Object obj) {this.threadId = id; this.lock = obj;} @ Override public void run () {synchronized (lock) {for (int I = 0; I < 100; + + I) {System.out.println ("Thread ID:" + this.threadId + ":" + I) } public class ThreadDemo {/ * * @ param args * @ throws InterruptedException * / public static void main (String [] args) throws InterruptedException {Object obj = new Object (); for (int I = 0; I < 10; + + I) {new Thread (new MyThread (I, obj)). Start (); Thread.sleep (1);}

From the second piece of code, we can see that the key to synchronization is that multiple Java thread objects compete for the same shared resource. In the above code, the shared resource is created externally and then passed to the Java thread. We can also take advantage of the fact that class member variables are shared by instances of all classes, so we can implement lock as a static member object, as shown in the following code:

Code

Package com.vista; class MyThread implements java.lang.Runnable {private int threadId; private static Object lock = new Object (); public MyThread (int id) {this.threadId = id;} @ Override public void run () {synchronized (lock) {for (int I = 0; I < 100; + + I) {System.out.println ("Thread ID:" + this.threadId + ":" + I) } public class ThreadDemo {/ * * @ param args * @ throws InterruptedException * / public static void main (String [] args) throws InterruptedException {for (int I = 0; I < 10; + + I) {new Thread (new MyThread (I)). Start (); Thread.sleep (1);} after reading the above, have you mastered the relationship between Java threads and Java? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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