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 to use the synchronized of Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to use the synchronized of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the synchronized of Java".

To understand the use of synchronized, you must first know what problem it is used to solve. Since synchronized means synchronization, of course it is to solve the problem of non-synchronization. Here is an example of an out-of-sync example to demonstrate possible problems.

In this example, we will create two thread classes. One is called TwoCounter, whose job is to accumulate two counter variables at the same time, starting from 1, you will immediately think, we are going to use it to achieve a synchronization. The other object is called Watcher, which, as the name implies, is used for monitoring. It is responsible for checking whether the values of two counters in the TwoCounter thread are equal, which seems to be meaningless, because since they are accumulated synchronously, how can the values of the two counters not be equal?

However, this is not the case. Let's look at the program first. Before looking at this program, it's best to flip through Think in Java 14.2.1. My program is actually simplified based on the example given in this section, where the main class is changed to Sharing2.

Class TwoCounter extends Thread {

Private int count1 = 0, count2 = 0

Private boolean started=false

Public void start () {

If (! started) file:// prevents the Start method from being called multiple times on a thread

{

Started=true

Super.start ()

}

}

Public void run () {

While (true) {

Count1++

File:// if TwoCounter runs at this point and the cpu time slice is assigned to Watcher, then the values of the two counters read by Watcher will of course be different. This possibility exists. " This is caused by the nature of threads-they can be suspended (paused) at any time. So between the execution times of the above two lines, execution pauses sometimes occur. At the same time, the Watcher thread happens to follow and compare at this very time, resulting in unequal counters. "(Think in Java)

Count2++

System.out.println ("Count1=" + count1+ ", Count2=" + count2)

Try {

Sleep (500)

} catch (InterruptedException e) {}

}

}

Public void synchTest () {

Sharing2.incrementAccess ()

If (count1! = count2)

System.out.println ("Unsynched"); / / once out of sync is found, display immediately

}

}

Class Watcher extends Thread {

Private Sharing2 p

Public Watcher (Sharing2 p) {

This.p = p

Start ()

}

Public void run () {

While (true) {

P.s.synchTest ()

Try {

Sleep (500)

} catch (InterruptedException e) {}

}

}

}

Public class Sharing2 {

TwoCounter s

Private static int accesSCOunt = 0

Public static void incrementAccess () {

AccessCount++

System.out.println ("accessCount=" + accessCount)

}

Public static void main (String [] args) {

Sharing2 aaa = new Sharing2 ()

Aaa.s=new TwoCounter ()

Aaa.s.start (); / / Open the TwoCounter thread

New Watcher (aaa); / / Open the Watcher thread

}

}

The above notes make it very clear that it is possible to be out of sync. But the strange thing is that when I am running, I never encounter an out-of-sync situation, so there is only one situation, that is, the count1++ and count2++ in the program are carried out almost at the same time, and the watcher thread cannot be plugged in, but why must there be an out-of-sync situation after the program above Think in Java runs? The principle of the two programs is exactly the same, the only difference is that my program is relatively simple, and runs on the command line, without using GUI. Is it because it is more expensive to run in Applet or windows main window, which makes watcher organic? So I tried to add a circular statement between count1++ and count2++ to artificially increase the gap, in order to make watcher easy to plug in, resulting in the situation that the monitored count1 is not equal to count2, and achieve non-synchronization. The modified program looks like this

.

Count1++

For (int iTuno Bandi)

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