The role of the Java Multithreading Keyword volatile is to indicate that multiple threads share this variable.
If it is read-only can be used directly, when writing data should pay attention to synchronization problems.
Example:
PackageCom.ming.thread.volatiletesttrhead1;/*** Volatile keyword use * volatile this keyword is used to keep variables modified by this keyword to be visible between multiple threads *@authorMingge **/ Public classVolatiletesttrheadextendsthread{volatile Public Static intcount; //If you remove the Synchronized keyword, it will cause the data to be out of sync synchronized Private Static voidAddcount () { for(inti=0;i<100;i++) {Count++; } System.out.println ("Count=" +count); } Public voidrun () {addcount (); }}
PackageCom.ming.thread.volatiletesttrhead1; Public classRun { Public Static voidMain (string[] args) {volatiletesttrhead[] Mythreadarr=Newvolatiletesttrhead[100]; for(inti=0;i<100;i++) {Mythreadarr[i]=NewVolatiletesttrhead (); } for(inti=0;i<100;i++) {Mythreadarr[i].start (); } }}
Hey...
Use of the Java Multi-threaded keyword volatile