Get the App
SLTechnology News&Howtos  ›  Development  › 

Introduction to the basic code of Java thread synchronization reference

Shulou Source: shulou.com Published: 2022-06-03 11:50:18 11月15日 Update

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!

Tags: Threads synchronization code learning programs runs content methods knowledge continuous multiple objects that is ideas situations articles more purposes knowledge points articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Docker Apple Microsoft MariaDB