Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thread with Lambda expression

I have an error at line 42 and 43 : Thread t1=new Thread(()->prod.test()); , Thread t2=new Thread(()->cons.test()); Unhandled exception type InterruptedException . If I try to quickfix it will created the try catch with an catch Exception, it will have the same error and will try to fix it in the same way continuing to surround it with try catch.

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

interface Predicate {
    public void test() throws InterruptedException;
}

class MyClass {
    int num = 0;
    Lock lock = new ReentrantLock();

    public void produce() throws InterruptedException {
        lock.lock();
        for (int i = 0; i < 1000; i++) {
            num++;
            Thread.sleep(1);
        }
        lock.unlock();
    }

    public void consume() throws InterruptedException {
        lock.lock();
        for (int i = 0; i < 1000; i++) {
            num--;
            Thread.sleep(1);
        }
        lock.unlock();
    }

    public int getNum() {
        return num;
    }

}

public class Main00 {

    public static void main(String[] args) throws InterruptedException {
        MyClass c = new MyClass();
        Predicate prod = c::produce;
        Predicate cons = c::consume;
        Thread t1 = new Thread(() -> prod.test());
        Thread t2 = new Thread(() -> cons.test());
        long start = System.currentTimeMillis();
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        long end = System.currentTimeMillis();
        System.out.println("time taken " + (end - start) + " num = "
                + c.getNum());
    }

}
like image 229
T4l0n Avatar asked Jul 14 '15 20:07

T4l0n


People also ask

Can we create thread using lambda expression?

By using a lambda expression, we can directly write the implementation for a method in Java. In the below program, we can create a thread by implementing the Runnable interface using lamda expression.

Are lambdas thread safe?

So - just like with methods - unless the lambda is stateful (that is, modifies or stores in fields or objects outside of the lambda), multiple invocations of the same lambda will not interfere and will be threadsafe.

How do I run a thread in Java 8?

Before starting a new thread you have to specify the code to be executed by this thread, often called the task. This is done by implementing Runnable - a functional interface defining a single void no-args method run() as demonstrated in the following example: Runnable task = () -> { String threadName = Thread.


3 Answers

You have created a functional interface Predicate whose method is declared to throw an InterruptedException, which is a checked exception. However, you call test() in the body of a lambda expression as the parameter to the Thread constructor that takes a Runnable, whose run() method is not declared to throw any checked exceptions. Therefore, because the exception is not caught in the body, a compiler error occurs.

Incidentally, it may be confusing to name your own interface Predicate, because of the built-in functional interface java.util.function.Predicate whose functional method returns a boolean.

Because run() can't throw an Exception, you must catch the exception and handle it. You might log the exception and its stack trace. You might wrap the exception in a RuntimeException. Either way, catching the checked exception will allow the code to compile. Example:

Thread t1 = new Thread(() -> {
    try {
        prod.test();
    } catch (InterruptedException e) {
        // handle: log or throw in a wrapped RuntimeException
        throw new RuntimeException("InterruptedException caught in lambda", e);
    }
});
like image 51
rgettman Avatar answered Oct 07 '22 07:10

rgettman


If you intend on running a single method only with no arguments you can replace the lambda with a method reference.

For instance:

Thread t = new Thread(() -> {
        foo();
    });

can be more succinctly expressed as

Thread t = new Thread(this::foo);

To start it

t.start();
like image 10
MeetTitan Avatar answered Oct 07 '22 05:10

MeetTitan


As @rgettman says, the name Predicate is unhappy... Anyways, you could take advantage of default methods in Java:

interface PredicateButPleaseChangeMyName {

    void test() throws InterruptedException;

    default void tryTest() {
       try {
          this.test();
       } catch (InterruptedException e) {
          // handle e (log or wrap in a RuntimeException)
       }
    }
}

Then, in your main method, simply create the threads by calling the default tryTest() method:

Thread t1 = new Thread(() -> prod.tryTest());
Thread t2 = new Thread(() -> cons.tryTest());
like image 3
fps Avatar answered Oct 07 '22 06:10

fps