Memorandum of Rookie Java (8)

zhaozj2021-02-16  40

Friday, January 24, 2003, is there a question in the 22nd note on the 22nd note? Why didn't I have a phenomenon that is not synchronized, I will send it to 9CBS, now I have basically solved this problem. Below is a summary of the recovery record of the forum

Reply to: bluesmile979 (laugh) () reputation: 100 2003-01-22 21:08:00 Score: 0 Talk about my opinion, I think the biggest problem is multithreading, watching your code, it seems to have only two A line is there. In the example, it should be more threads, and multiple threads competition time, and the chances of interruption will naturally be much larger. Even if you add a cycle, due to the machine's calculation speed, there is still no multiple thread competition, then it is obvious. I don't know how everyone thinks.

Reply to: xm4014 (forrest) () reputation: 100 2003-01-22 22:07:00 Score: 0 to bluesmile979 (smile) I have also thought of whether it was caused by too little thread, but I will Think in Java The two parameters in the routines are set to 1 to run, so in the same time, only two threads I wrote, and the results are not synchronized. How to explain this again?

Reply to: tianfeichen (lateral ear listening) () reputation: 110 2003-01-22 23:57:00 Score: 0 Thread arrangements are random, rarely there will be no synchronization, and there is less than discovery. What is commonly used is, first let the infinite loop start, when the loop is not required, only one stop condition, such as: if (counter1! = Counter2) {system.out.println (Counter1 ", " counter2) system.exit (0);} The rest is waiting, generally more than ten seconds or even a few seconds, can find that the count has reached a few hundred thousand or millions. If you open 5 threads at the same time, I have been waiting for more than one minute, I am a synchronization.

My method may not be scientific, but the effect is very good.

Reply to: xm4014 (forrest) () Reputation: 100 2003-01-23 11:44:00 Score: 0 Can you help me debug? Why didn't I get the result in accordance with your method? The following code is directly copied, the program is sharing2.java, version is 1.4.1class twocounter extends thread {private int count1 = 0, count2 = 0; private boolean started = false; public void start () {IF ! start) {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 {pssynchtest (); 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 what you mean, my program is no problem, just less thread, it is difficult to generate different steps. When you have to increase the increase in COUNTER, it is possible, right?

Reply to: hey_you (hey) () Reputation: 100 2003-01-23 13:27:00 Score: 0

I think so: A conflict in different steps is a possibility, and Sychronize is 0. You don't have 1 discovery, and you can't prove that there will never happen, that's just a matter of time. The system's scheduling of threads is affected by the environment. If you have ran a lot of programs on your machine, it may be different.

Reply to: xm4014 (forrest) () Reputation: 100 2003-01-23 15:56:00 Score: 0 huh, I run the program with Tianfeichen's method, that is, the code I have on the same results actually , Counter1 = 217327, Counter2 = 217356, really thinks a lot. But the time is quite simple for more than one or two hours. At least two hours, it may be caused by my life environment. As Hey_You (Hey) said, just a time problem.

I hope that other people can give more views. If you feel that there is no need to discuss it, then I will post.

Reply to: bluesmile979 (laugh) () reputation: 100 2003-01-23 16:38:00 Score: 0 I test, you can stick to it, you can take it.

I think the problem is two points. One is the number of threads I think

The other is that the setText you think will have more processing, which takes up more resources.

Both situations affect the chance of appearance of this problem :) The land dominate, summarize it, huh, huh.

Reply to: LINLIANGYI (Blue Mountain Coffee) () Reputation: 100 2003-01-23 17:10:00 Score: 0 SLEEP (500) Time to win the time of FOR (5000), so thread is switched in SLEP The probability is far better than the probability of being interrupted in For. ! (Looking back at my program, I know) !

By the way, the Swing and AWT controls are operated in the thread, such as settext, often cause a lot of accidental landlords to see related books! !

Reply to: xm4014 (forrest) () reputation: 100 2003-01-24 14:25:00 Score: 0 I integrate everyone's views:

First of all, if you don't use the synchronized keyword to define synchronous methods or define synchronization blocks, then you may have absolute existence, in turn, Synchronized is 0.

In the first case, it may be in the case where it may be present, but its chance is affected by the following aspects. 1. In different operating systems and operating environments, the chance of capturing disagree will be different, Or waiting for the time may have a short bit 2. There is a lot of threads in the program. If the thread is too small, then this difference is difficult to capture, and it may take a long time 3. The impact of the code itself. Using the AWT class involves the GUI method, there may be more resources, causing a lot of accidents, then the possibility of conflicts is much larger. 4. Thread is randomly assigned by the operating system, there is uncertain Sex, this uncertainty will affect the last result

I don't know if it is correct, what other supplements do you have?

But I don't understand, why is the last result, Counter1 (217327) and Counter2 (217356) will be so much different. According to my program, even if the Watcher thread is inserted into two self-added statements, detect The difference between the two counters is 1. There is such a big difference, and it is only possible to be a record of the self-portrait of a counter. There are many times in the case that it is not running. This It's too horrible! Although there are other threads will interfere with the current thread, but you will not let the current thread sentence don't run, and you will wait again. I am a bit confused, the operating system is not learned, if everyone is not Talking about it, let me explain it.

As a result, now there is a new problem, I want to wait until tomorrow, I have an answer.

But we can solve another question that involves synchronized. This is a post I saw on the forum. It is because I can't solve it, I think there is necessary to look back to study the thread and synchronization. Question as follows:

File: // Analysis of this procedure and explains, 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? {Try {system.out.println ("Waiting for B To Complete ...); b.wait (); // What is the meaning of this sentence, let Who 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 () {system.out.println ("Threadb is running .."); for (int i = 0; i <100; i ) {Total = i System.out.println ("Total IS" Total;} NOTIFY ();}}}

To analyze this procedure, you must first understand notify () and wait (), why didn't you record these two methods when you record threads a few days ago, because these two methods are not a Thread class, but belongs to the bottom. Object basic class, that is, not only Thread, each object has NOTINE and WAIT features, why? Because they are used to manipulate locks, and each object is locked, the lock is the basis of each object, since the lock is based, the method of manipulating lock is of course the most basic.

Before you look down, you should first be better to review the 3.3.1 of Think in Java 14.3.1: Waiting and notice, that is, Wait () and Notify.

Follow the explanation in Think in Java: "Wait () allows us to place the thread into the" sleep "state, and" actively "wait for the conditions to change. And only in a NOTIFY () or NotifyAll () when it changes, The thread will be awakened and check if the condition has changed. "

Let's explain this sentence. "WAIT () allowed us to place the thread into" Sleep "status", that is, wait is also blocked by the current thread, this is the same as Sleep or Suspend. That and SLEEP, What is SUSPEND?

Differences between "Wait" and "actively" wait for the conditions to change ", this is critical, Sleep and Suspend cannot be done. Because we sometimes need to control the conflicts between threads through syncing (SYNCHRONIZED). Once you use synchronization, you have to lock the object, which is to get the object lock. Other threads to use the object lock can only queue waiting, waiting until the synchronization method or the program in the synchronization block is completely operational. Method in synchronization method In sync block, no matter how Sleep () is still unlocked when you are called, they all account for the object lock that is being used. And Wait can, it can make synchronization methods or synchronization blocks temporarily Abandon the object lock, and temporarily let other people who need object locks (here should be blocks, or threads), which means that other synchronization methods can be called during WAIT () period! Next (Sleep, Suspend), this is impossible. But note that I have said before, just give up the object lock, temporarily give other threads, my WAIT is still to lock this object. Wait what? Is Wait others used to finish it! Ok, how can I lock the object? The first method is limited to the time to borrow. In Wait (), the parameters are set, such as Wait (1000), in milliseconds, indicate that I only borrowed 1 second After a second, I automatically recovered. The second method, let the people who borrow go not know me, he is finished, I have to give it to me. At this time, I will pick up immediately. Oh, if I set up 1 hour After receiving, others are only half an hour, what should I do? I will rely on it! Of course, I will return it, I still have to set up how long it is. So how do you notify me? I believe everyone can think of it. Notify (), this is the last sentence "and only the thread will be awakened when a notify () or notifyAll () changes. So we can put a wait () and notify () Any synchronization method or inside the synchronization block is placed, whether or not the process involving the thread is prepared in that class. And in fact, we can only call WAIT () and Notify () in synchronous methods or synchronous blocks.

At this time, we will explain the above procedures, which is easy to be confused.

Synchronized (b) {...}; what is meaning is to define a synchronization block that uses B as a resource lock. B.Wait (); meaning temporary release lock, and blocks the current thread, so that other threads using the same lock have the opportunity to execute, here to use the same lock is the B thread itself. This thread is executed After the place, use notify () to inform WAIT threads, the lock has been used, and after the synchronization block where the notify () is running, the thread where the WAIT is located will continue.

转载请注明原文地址:https://www.9cbs.com/read-27538.html

New Post(0)