이 코드가 있지만 incrementOnly에 대한 메서드 호출이있는 부분에서 37 ~ 43 행을 이해하지 못합니다.람다 표현식은 Java에서 어떻게 작동합니까?
Heres는 내 이해 (이 맞습니까?) T2 그냥 그때는 방법 incrementOnly를 호출 라인 (36)에 새로운 스레드를 생성합니다 줄에서 35
T3를 새 스레드를 생성합니다.
그런 다음 41 행에서 t2에 대해 실행 메소드가 실행됩니다. 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();
}
}
우수 설명. 감사! – NoMoreErrors