【问题标题】:How does lambda expressions work in Java?lambda 表达式在 Java 中是如何工作的?
【发布时间】:2016-11-25 02:47:10
【问题描述】:

我有这段代码,但我不明白第 37 行到第 43 行中存在对 incrementOnly 的方法调用的部分。

这是我的理解(这是正确的吗?) t2 将在第 35 行创建一个新线程

t3 将在第 36 行创建一个新线程,然后调用方法 incrementOnly。

然后在第 41 行,将执行 t2 的 run 方法。在第 42 行,将在 t3 执行 run 方法。

package aa.race;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ThreadDemoCounter implements Runnable
{
    int counter;
    int alternate;
    String name;


    public static  int numLoops = 4;
    public static  int numPrints = 1500;

    public ThreadDemoCounter(String n)

    {
        name = n;
        counter = 0;
    }


    // For bonus -- delete method go.  Change main to below code:
    public static void main(String[] args) throws Exception
    {
        ThreadDemoCounter c1 = new ThreadDemoCounter("c1");
        //Run the multithreaded demo a few times
        for (int foo = 0; foo < numLoops; foo++)
        {
            c1.counter = 0;

            Thread t1 = new Thread(c1);
            Thread t2 = new Thread(c1);

            Thread t3 = new Thread(c1::incrementOnly);
            Thread t4 = new Thread(c1::incrementOnly);

            t1.start();
            t2.start();
            t3.start();
            t4.start();


            t1.join();
            t2.join(); //wait for both
            t3.join();
            t4.join(); //wait for both

            System.out.println("c1 = " + c1.counter);
            System.out.println("===== end loop =====");
        }

    }


    public void incrementOnly()
    {
        for (int i =0 ; i < numPrints; i++)
        {
            incrementCounter();
        }
    }

    public void run()
    {

        for (int j = 0; j < numPrints; j++)
        {


            LockFactory.getLock(name).lock();
            System.out.println("counter " + name + " = " + getCounter() + " retrieved by thread: " + Thread.currentThread().getName());

            incrementCounter();
            LockFactory.getLock(name).unlock();

        }
        System.out.println();
    }

    public int getCounter()
    {
        return counter;
    } //start at 0

    public void incrementCounter()
    {
        LockFactory.getLock(name).lock();

        counter++;
        LockFactory.getLock(name).unlock();
    }
}

【问题讨论】:

    标签: java multithreading concurrency java-threads


    【解决方案1】:

    所有 4 个构造函数调用都在调用Thread(Runnable target),其中Runnable@FunctionalInterface,方法为void run()。当线程启动时,会调用Runnablerun()方法。

    前两个构造函数调用new Thread(c1) 传递ThreadDemoCounter 的一个实例,因此这两个线程将为c1 实例调用ThreadDemoCounter.run() 方法。

    另外两个构造函数调用将method reference 传递给c1incrementOnly() 方法。这是一个有效的方法,因为它也是一个无参数的 void 方法。这两个线程将为c1 实例调用ThreadDemoCounter.incrementOnly() 方法。

    总共有 4 个线程在运行,其中两个执行 run() 方法,其中两个执行 incrementOnly() 方法,都在 ThreadDemoCounter 的同一个实例上,即 c1

    仅供参考:该代码中没有 lambda expressionsmethod reference expression 不是 lambda 表达式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-10
      • 2011-04-24
      • 2014-03-23
      • 2013-06-30
      • 2019-01-16
      • 2015-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多