के साथ थ्रेड मुझे लाइन 42 और 43: Thread t1=new Thread(()->prod.test());
, Thread t2=new Thread(()->cons.test());
अनचाहे अपवाद प्रकार इंटरप्टेड एक्सेप्शन पर कोई त्रुटि है। यदि मैं क्विकफ़िक्स करने का प्रयास करता हूं तो यह कैच अपवाद के साथ पकड़ने का प्रयास करेगा, इसमें एक ही त्रुटि होगी और इसे उसी तरह से ठीक करने की कोशिश की जाएगी जिससे इसे पकड़ने के साथ घूमना जारी रखा जा सके।लैम्ब्डा अभिव्यक्ति
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());
}
}