java - java 多线程间共享对象

标签 java multithreading

我希望能够同时运行两个依赖同一个全局变量的方法。第一个方法定期更新共享变量,但永远不会完成运行。第二种方法记录时间。当时间耗尽时,第二个方法返回第一个方法共享变量的最后结果。下面是我到目前为止所得到的,在我需要帮助的地方注释掉了伪代码。

package learning;

public class testmath{
    public static void main(String[] args){

        long finishBy = 10000;
        int currentresult = 0;

        /*
         * run eversquare(0) in  a seperate thread /in parallel
         */


        int finalresult = manager(finishBy);
        System.out.println(finalresult);
    }


    public static int square(int x){
        return x * x;
    }

    public static void eversquare(int x){
        int newresult;
        while(2 == 2){
            x += 1;
            newresult = square(x);
            /*
             * Store newresult as a global called currentresult
             */

        }
    }



    public static int manager(long finishBy){


        while(System.currentTimeMillis() + 1000 < finishBy){
            Thread.sleep(100);
        }

        /*
         * Access global called currentresult and create a local called currentresult
         */     
        return currentresult;

    }

}

最佳答案

您只需要运行一个额外的线程:

public class Main {
  /**
   * Delay in milliseconds until finished.
   */
  private static final long FINISH_BY = 10000;
  /**
   * Start with this number.
   */
  private static final int START_WITH = 1;
  /**
   * Delay between eversquare passes in milliseconds.
   */
  private static final long DELAY_BETWEEN_PASSES = 50;
  /**
   * Holds the current result. The "volatile" keyword tells the JVM that the
   * value could be changed by another thread, so don't cache it. Marking a
   * variable as volatile incurs a *serious* performance hit so don't use it
   * unless really necessary.
   */
  private static volatile int currentResult = 0;

  public static void main(String[] args) {
    // create a Thread to run "eversquare" in parallel
    Thread eversquareThread = new Thread(new Runnable() {
      @Override public void run() {
        eversquare(START_WITH, DELAY_BETWEEN_PASSES);
      }
    });

    // make the eversquare thread shut down when the "main" method exits
    // (otherwise the program would never finish, since the "eversquare" thread
    // would run forever due to its "while" loop)
    eversquareThread.setDaemon(true);

    // start the eversquare thread
    eversquareThread.start();

    // wait until the specified delay is up
    long currentTime = System.currentTimeMillis();
    final long stopTime = currentTime + FINISH_BY;
    while (currentTime < stopTime) {
      final long sleepTime = stopTime - currentTime;
      try {
        Thread.sleep(sleepTime);
      } catch (InterruptedException ex) {
        // in the unlikely event of an InterruptedException, do nothing since
        // the "while" loop will continue until done anyway
      }
      currentTime = System.currentTimeMillis();
    }
    System.out.println(currentResult);
  }

  /**
   * Increment the value and compute its square. Runs forever if left to its own
   * devices.
   *
   * @param startValue
   * The value to start with.
   *
   * @param delay
   * If you were to try to run this without any delay between passes, it would
   * max out the CPU and starve any other threads. This value is the wait time
   * between passes.
   */
  private static void eversquare(final int startValue, final long delay) {
    int currentValue = startValue;
    while (true) { // run forever (just use "true"; "2==2" looks silly)
      currentResult = square(currentValue); // store in the global "currentResult"
      currentValue++; // even shorter than "x += 1"
      if (delay > 0) {
        try { // need to handle the exception that "Thread.sleep()" can throw
          Thread.sleep(delay);
        } catch (InterruptedException ex) { // "Thread.sleep()" can throw this
          // just print to the console in the unlikely event of an
          // InterruptedException--things will continue fine
          ex.printStackTrace();
        }
      }
    }
  }

  private static int square(int x) {
    return x * x;
  }
}

我还应该提到“ volatile ”关键字适用于(大多数)原语,因为您现在看到的任何 JVM 都保证它们将被原子地修改。对象的情况并非如此,您需要使用同步块(synchronized block)和锁来确保它们始终以一致的状态“被看到”。

大多数人还会提到,您确实不应该在方法本身上使用synchronized关键字,而应该在特定的“锁定”对象上进行同步。一般来说,这个锁对象在代码之外不应该是可见的。这有助于防止人们错误地使用您的代码,给自己带来麻烦,然后试图责怪您。 :)

关于java - java 多线程间共享对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24271977/

相关文章:

java - 如何解决java.lang.RuntimeException : Unable to start activity ComponentInfo

android - 与 HandlerThread 通信的示例

c++ - my_thread_global_end 线程没有退出,错误?

java - org.hibernate.HibernateException : Don't change the reference to a collection with cascade ="all-delete-orphan"

java - 在服务器端有一个后台线程定期更新请求使用的一些数据的最佳实践?

java - 加密应用程序数据的正确方法

python - 使用线程在 ZeroMQ REQ/REP 模式的服务器端创建许多回复器套接字

c - 增加 xcode 上的线程数会增加程序时间

python - 并行化Python列表理解并没有提高性能

java - 获取亚马逊所有地区的实例