使多个线程使用并更改相同的变量 [英] Make multiple threads use and change the same variable

查看:158
本文介绍了使多个线程使用并更改相同的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中我需要使用多个线程并编辑相同的变量,但它似乎不起作用。这是我的意思的一个例子,这将是我的主要类。

in my program I need to have multiple threads use and edit the same variable, but it doesn't seem to be working. Here is an example of what I mean, this 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();
    }
  }
}

这将是我的Thread类:

This would 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);
  }
}

我当场写了这段代码,所以有可能是一些错误,但没关系。我的程序基本上需要做这样的事情,但不是每次打印数字加1,所有线程只是打印相同的数字,0,多次。请帮助我,谢谢。

I wrote this code on the spot, so there may be some errors, but thats ok. My program basically needs to do something like this, but instead of printing the number plus 1 every time, all the threads simply print the same number, 0, multiple times. Please help me, thanks.

推荐答案


在我的程序中我需要使用和编辑多个线程相同的变量,但它似乎没有工作......

In my program I need to have multiple threads use and edit the same variable, but it doesn't seem to be working...

任何时候多个线程正在更新你需要的同一个变量担心内存同步。线程获得高性能的方法之一是因为每个线程都使用本地CPU内存缓存,因此可能使用过时的变量副本。您需要使用 synchronized volatile 关键字来强制线程的缓存将任何更新写入中央存储或更新其来自中央的缓存。

Anytime multiple threads are updating the same variable you need to worry about memory synchronization. One of the ways that threads get high performance is because each thread utilizes the local CPU memory cache and so may be working with stale copies of variables. You need to use the synchronized or volatile keywords to force the thread's cache to write any updates to central storage or update its cache from central.

虽然这会处理内存同步,但它并不一定能保护您免受竞争条件的影响。同样重要的是要意识到 ++ 实际上是3个操作:获取当前值,增加它并再次存储它。如果多个线程试图这样做,可能会导致线程竞争条件要错过的 ++ 操作。

Although this takes care of memory synchronization, it doesn't necessarily protect you from race conditions. It is also important to realize that ++ is actually 3 operations: get the current value, increment it, and store it back again. If multiple threads are trying to do this, there are thread race-conditions which can cause the ++ operations to be missed.

在这种情况下,你应该使用 AtomicInteger 包装 volatile int 字段的类。它为您提供了诸如 incrementAndGet()之类的方法,它们以线程安全的方式完成递增该字段的工作。

In this case, you should use the AtomicInteger class which wraps a volatile int field. It gives you methods like incrementAndGet() which do the job of incrementing that field in a thread-safe manner.

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

多个线程可以安全地递增相同的变量。

Multiple threads can then be incrementing the same variable safely.

这篇关于使多个线程使用并更改相同的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆