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());
}
}
You have created a functional interface
Predicate
whose method is declared to throw anInterruptedException
, which is a checked exception. However, you calltest()
in the body of a lambda expression as the parameter to theThread
constructor that takes aRunnable
, whoserun()
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 interfacejava.util.function.Predicate
whose functional method returns aboolean
.Because
run()
can't throw anException
, you mustcatch
the exception and handle it. You might log the exception and its stack trace. You might wrap the exception in aRuntimeException
. Either way, catching the checked exception will allow the code to compile. Example:As @rgettman says, the name
Predicate
is unhappy... Anyways, you could take advantage ofdefault
methods in Java:Then, in your main method, simply create the threads by calling the default
tryTest()
method:If you intend on running a single method only with no arguments you can replace the lambda with a method reference.
For instance:
can be more succinctly expressed as