java - Why does the join() now work here? -


I am trying to sum all numbers from 1 to n with the help of multiple threads.

This is my thread:

  The public class executes MyThread Runnable {// limitations int lowerLimit; Int upper limit; MyInteger results; // Constructor public MyThread (int less limit, int extract, myintase result) {this.lowerLimit = lowerLimit; This.upperLimit = Upper Limit; This.result = results; } @ Override lists the numbers in the given time for public wide run () {// (int i = less limit; i & lt; = upper limit i i ++) {result.add (i); }}}  

This gets an upper and lower border and results that are shared on all threads.

This is my test class:

  The amount of public square {public static long surrenders (int number threads, int ann) {thread} thread = new thread [ Numbertrareds]; MyInteger Results = New MyInteger (0); // creates new threads and starts them (int i = 0; i  

If I only use one thread, then the result is correct. If I use many threads, the result is sometimes correct and most of the time is very low - meaning that the thread has not yet counted. But how is this possible? Waiting for each for-loop to wait for me so that the method first eliminates the result when all threads have calculated their share.

Best Regards

Edit:

Edit:

> The MyInteger class looks like this:

Public class MyInteger {private long value;

  Public MyInteger (int value) {this.value = value; } Public long time value () {return value; } Public Zero Set Value (int value) {this.value = value; } Public Zero Addition (int summand) {value + = summand; } Public zero sub (int decr) {value - = decr; } Public Zero Enhance () {value ++; } Public Zero reduction () {value--; }}  

Your join code looks correct I suspect That your code is facing hazard in race :

When If you move more than one thread, modify all the shared resources, then you have to make sure that it is being modified while they have exclusive access.

As a quick fix To confirm that this is a data race, try to add synchronize modifier to your add (

Thread 1 call addition (3)

  • Li> JVM (Thread 1) reads that Myintager has a value of 17, and puts it separately during operation by adding
  • Thread 2 call add (5)
  • JVM (Thread 2) reads that MyInteger has a value of 17, and Adds it separately during the joint operation
  • JV M (Thread 1 and thread 2) increase their respective values, so in thread 1 20, and thread 2 is 22.
  • JVM (Thread 1) writes 20 inside the primitive MyInteger
  • writes JVM (Thread 2) 22 for the primitive inside MyInteger
  • < P> Now << / strong> << / strong> > add (3) operation, as well as the form of atomic operations as a final value of 25 Would result in

    The most important thing to remove it, with the importance of thread protection on one side, is that the primitives connected even a simple extra action single, nuclear operation Is not done as. Instead, it has broken into JVM within smaller stages, which allows the concurrent thread to race against each other.

    Here is a screenshot of the (Excellent) table, the wiki article which shows visually:


    Comments

    Popular posts from this blog

    java - org.apache.http.ProtocolException: Target host is not specified -

    java - Gradle dependencies: compile project by relative path -

    ruby on rails - Object doesn't support #inspect when used with .include -