In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to solve the problem of Java thread asynchrony". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to solve the phenomenon of Java thread out of synchronization"!
After all, the arrangement of threads is random, there will be few out-of-sync, less number of times is not easy to find.
My usual method is to let the infinite loop start, and the loop does not require output, but only adds a stop condition, such as:
If (counter1! = counter2)
{
System.out.println (counter1 + "," + counter2)
System.exit (0)
}
The rest is to wait, usually more than ten seconds or even a few seconds to get the result, you can find that the count has reached hundreds of thousands or millions.
If I open five threads at the same time and wait a minute, I will consider it synchronized.
My method may not be very scientific, but it works well.
Just copy the following code directly. The program is called Sharing2.java, version 1.4.1.
Class TwoCounter extends Thread {
Private int count1 = 0, count2 = 0
Private boolean started=false
Public void start () {
If (! started)
{
Started=true
Super.start ()
}
}
Public void run () {
While (true) {
Count1++
Count2++
/ / System.out.println ("Count1=" + count1+ ", Count2=" + count2)
Try {
Sleep (500)
} catch (InterruptedException e) {System.out.println ("TwoCounter.run");}
}
}
Public void synchTest () {
/ / Sharing2.incrementAccess ()
If (count1! = count2)
{System.out.println (count1+ "," + count2)
System.exit (0)
}
}
}
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) {System.out.println ("Watcher.run");}
}
}
}
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 ()
New Watcher (aaa)
}
} /: ~
In addition, according to you, there is no problem with my program, but with fewer threads, it is difficult to produce non-synchronization, which is not possible until the number of counter increases to a large number, right?
I think of it this way: out of sync and conflict is a possibility, and sychronize makes it zero. If you don't find out that it's out of sync, it doesn't prove that it will never happen. It's just a matter of time. The system's scheduling of threads is affected by the environment, and if you have a lot of programs running on your machine at the same time, the situation may be different.
Hehe, the program I run with the tianfeichen method, that is, the code I posted on it, actually has a result. Counter1= 217327 counter217356, I really want to be a lot worse. But the time is not as simple as a minute or two, at least two hours later, it may be due to the different operating environment between me and him. As hey_you (Hey) said, it is only a matter of time.
If I take the exam, you can hold on for an hour or two.
I think the result of the problem will be two o'clock. One is what I think is the number of threads.
The other is that you think setText will have more processing and take up more resources.
Both situations will affect the probability of this problem:) the master of the building will sum up, hehe.
Sleep takes more time than for (5000), so threads are much more likely to be switched in sleep than interrupted in for! (just look back at my program.)
In fact, the fact that two variables are not equal to equal means that they are out of sync!
By the way, it often causes a lot of accidents when swing and awt controls operate in threads, such as setText.
Let me sum up your views:
First of all, it is certain that if you do not use the synchronized keyword to define synchronization methods or synchronization blocks, then the possibility of non-synchronization is absolute. On the contrary, synchronized makes this possibility 0. 5%.
In the first case, although the possibility of asynchrony exists, its probability is affected by the following factors
1. In different operating systems and operating environments, the probability of catching out of sync may be different, or the waiting time may be long or short.
two。 The number of threads in the program, if there are too few threads, then this kind of non-synchronization is difficult to capture and may take a long time to wait.
3. The influence of the code itself. For example, using methods that involve GUI in the awt class may take up more resources and cause a lot of accidents, so conflicts are much more likely to occur.
4. Threads are randomly assigned by the operating system, and there is inherent uncertainty, which will also affect the final result.
I don't know if it's correct, but what else do you have to add?
The official post will be closed tomorrow.
But to be honest, I don't understand why the final result, counter1 (217327) and counter2 (217356), is so different. According to my program, even if the watcher thread inserts between two self-adding statements, the difference detected between the two counters is at most 1. With such a big difference, it is only possible that the self-adding statement of a counter has been forcibly interrupted many times without running at all. This is horrible! Although the existence of other threads will interfere with the current thread, it will not allow the current thread statement not to run, at best, it will wait and run again. I'm a little confused. I didn't learn the operating system well. If you don't mind the trouble, please explain it for me.
As a result, there are new questions now, and I think we won't have an answer until tomorrow.
But we can solve another problem involving synchronized today. This is a post I saw on the forum. It was because I couldn't solve it that I thought it was necessary to go back to threading and synchronization.
The questions are as follows:
File:// analysis of this program, and explain, focusing on synchronized, wait (), notify thank you!
Class ThreadA
{
Public static void main (String [] args)
{
Threadb b=new ThreadB ()
B.start ()
System.out.println ("b is start....")
Synchronized (b) / / what is the meaning of b in parentheses and what does it do?
{
Try
{
System.out.println ("Waiting for b to complete...")
B.wait (); / / what is the meaning of this sentence, and who is wait?
System.out.println ("Completed.Now back to main thread")
} catch (InterruptedException e) {}
}
System.out.println ("Total is:" + b.total)
}
}
Class ThreadB extends Thread
{
Int total
Public void run ()
{
Synchronized (this)
{
System.out.println ("ThreadB is running..")
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.