In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the basic code introduction of Java thread synchronous reference". The content of the article is simple and clear, and it is easy to learn and understand. let's study and learn the basic code introduction of Java thread synchronous reference.
Java thread synchronization references have many uses, and we have a lot to learn in constant use. We can run all kinds of computer software programs on computers. Each running program may include multiple threads that run independently.
Public class SynTest... {private String firstName, lastName; private synchronized void setName (String firstName, String lastName)... {print ("entering setName"); this.firstName = firstName; print ("Set first name have done firstName=" + this.firstName); try... {Thread.sleep (1000);} catch (InterruptedException e). {} this.lastName = lastName Print ("set last name have done,and leave setName () method.firstName=" + this.firstName + "lastName=" + this.lastName);} private void print (String msg)... {String thread = Thread.currentThread (). GetName (); System.out.println (thread + ":" + msg);} public static void main (String [] args). {/ / must be declared final, otherwise the run () method in runnable cannot be accessed. Final SynTest test1 = new SynTest (); final SynTest test2 = new SynTest (); Runnable run1 = new Runnable ()... {public void run ()... {test1.setName ("arzu", "guli");}}; Thread threadOne = new Thread (run1, "threadOne"); threadOne.start (); try. {Thread.sleep } catch (InterruptedException e). {} Runnable run2 = new Runnable (). {public void run (). {/ / if the thread references the object test2, the setName method does not need synchronization to ensure that the program achieves the desired goal. Test1.setName ("kang", "midi");}}; Thread threadTwo = new Thread (run2, "threadTwo"); threadTwo.start (); System.out.println ("main () exit") }} Thank you for your reading. The above is the content of "basic Code introduction of Java Thread synchronization reference". After the study of this article, I believe you have a deeper understanding of the basic code introduction of Java thread synchronization reference. the specific use also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.