Use multiple threads and change the same variable

in my program, I need to use multiple threads and edit the same variable, but it does not seem to work. Here is an example of what I mean, it would be my main class.

public class MainClass { public static int number = 0; public static String num = Integer.toString(number); public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter number of threads."); int threads = in.nextInt(); for (int n = 1; n <= threads; n++) { java.lang.Thread t = new Thread(); t.start(); } } } 

This will be my Thread class:

 public class Thread extends java.lang.Thread { public void run() { MainClass.number++; System.out.println("Thread started"); System.out.println(MainClass.num); } } 

I wrote this code in place, so there may be some errors, but this is normal. My program should basically do something like this, but instead of printing the number plus 1 each time, all threads just print the same number 0, several times. Please help me, thanks.

+4
source share
2 answers

In my program, I need to use multiple threads and edit the same variable, but it does not seem to work ...

At any time, when several threads update the same variable, you need to worry about memory synchronization. One of the ways that threads get high performance is that each thread uses the local CPU cache and therefore can work with legacy copies of variables. You need to use the synchronized or volatile keywords to force the stream cache to write any updates to the central repository or to update its cache from the center.

Although this will take care of memory synchronization, it does not necessarily protect you from race conditions. It is also important to understand that ++ is actually 3 operations: get the current value, increase it and save it again. If several threads try to do this, there are conditions for the thread race , which can lead to the omission of ++ operations.

In this case, you should use the AtomicInteger class, which wraps the volatile int field. It provides you with methods like incrementAndGet() that perform the task of increasing this field in a thread-safe manner.

 public static AtomicInteger number = new AtomicInteger(0); ... MainClass.number.incrementAndGet(); 

Multiple threads can then safely increment the same variable.

+9
source

Here you go ...

  import java.util.Scanner; import java.util.concurrent.atomic.AtomicInteger; public class UpdateVariables { static int num = 0; public static AtomicInteger atomicInteger = new AtomicInteger(num); @SuppressWarnings("resource") public static void main(String args[]) { Scanner userInput = new Scanner(System.in); System.out.println("Enter Number of Threads: "); int getThreadNumber = userInput.nextInt(); for(int i = 0; i < getThreadNumber; i++) { PrintThread p = new PrintThread(); p.start(); } } } class PrintThread extends Thread { public void run() { System.out.println("Thread Started: "); System.out.println(UpdateVariables.atomicInteger.incrementAndGet()); } } 
0
source

All Articles