இயக்கக்கூடிய சிக்கல்
Runnable interface மற்றும் அதை செயல்படுத்தும் Thread class உங்களுக்கு ஏற்கனவே தெரிந்திருக்கும் . இந்த இடைமுகம் எப்படி இருக்கும் என்பதை நினைவு கூர்வோம்:
public interface Runnable {
public abstract void run();
}
ரன் முறையின் திரும்பும் வகை வெற்றிடமானது என்பதை நினைவில் கொள்ளவும் . ஆனால் ஒரு எண், ஒரு சரம் அல்லது வேறு ஏதேனும் ஒரு பொருளின் வடிவத்தில் அதன் செயல்பாட்டின் சில முடிவுகளைத் தருவதற்கு நம் நூல் தேவைப்பட்டால் என்ன செய்வது? பின்னர் நாம் ஒரு தீர்வைக் கொண்டு வர வேண்டும். இந்த மாதிரி ஏதாவது:
public class Fibonacci implements Runnable {
private final int index;
private int result;
public Fibonacci(int index) {
this.index = index;
}
@Override
public void run() {
int first = 0;
int second = 1;
if (index == 1) {
result = first;
} else if (index == 2) {
result = second;
} else {
for (int i = 0; i < index - 2; i++) {
int temp = second;
second += first;
first = temp;
}
result = second;
}
}
public static void printByIndex(int index) throws InterruptedException {
Fibonacci fibonacci = new Fibonacci(index);
Thread thread = new Thread(fibonacci);
thread.start();
thread.join();
System.out.println("Fibonacci number " + index + ": " + fibonacci.result);
}
}
பின்வரும் முக்கிய முறையை இயக்குவோம் :
public static void main(String[] args) throws Exception {
Fibonacci.printByIndex(10);
}
பணியகம் காண்பிக்கும்:
இந்த குறியீடு பல குறைபாடுகளைக் கொண்டுள்ளது. எடுத்துக்காட்டாக, சேரும் முறைக்கான அழைப்பின் விளைவாக , printByIndex முறை செயல்படுத்தப்படும்போது பிரதான நூல் தடுக்கப்படும் .
அழைக்கக்கூடிய இடைமுகம்
இப்போது Java நமக்கு வழங்கும் இடைமுகத்தைப் பார்ப்போம், இது Runnable க்கு மாற்றாகப் பயன்படுத்தப்படலாம் . இது அழைக்கக்கூடிய இடைமுகம்:
public interface Callable<V> {
V call() throws Exception;
}
நீங்கள் பார்க்க முடியும் என, Runnable போலவே , இதற்கு ஒரே ஒரு முறை மட்டுமே உள்ளது. இந்த முறை ரன் முறையின் அதே நோக்கத்திற்காக உதவுகிறது - இது ஒரு இணையான நூலில் செயல்படுத்தப்படும் குறியீட்டைக் கொண்டுள்ளது. வேறுபாடுகளைப் பொறுத்தவரை, திரும்பும் மதிப்பைப் பாருங்கள். இப்போது இடைமுகத்தை செயல்படுத்தும்போது நீங்கள் குறிப்பிடும் எந்த வகையாகவும் இருக்கலாம்:
public class CurrentDate implements Callable<Long> {
@Override
public Long call() {
return new Date().getTime();
}
}
மற்றொரு உதாரணம்:
Callable<String> task = () -> {
Thread.sleep(100);
return "Done";
};
இங்கே பயனுள்ள வேறு ஒன்று உள்ளது — அழைப்பு முறை விதிவிலக்கு அளிக்கலாம் . அதாவது, ரன் முறையைப் போலன்றி, அழைப்பு முறையில், முறையின் உள்ளே ஏற்படும் சரிபார்க்கப்பட்ட விதிவிலக்குகளை நாம் கையாள வேண்டியதில்லை:
|
|
எதிர்கால இடைமுகம்
Callable உடன் நெருக்கமாகச் செயல்படும் மற்றொரு இடைமுகம் எதிர்காலம் . எதிர்காலமானது ஒத்திசைவற்ற (இணை) கணக்கீடுகளின் முடிவைக் குறிக்கிறது ( அழைப்பு முறையால் வழங்கப்படும் மதிப்பு). கணக்கீடுகள் முடிந்ததா என்பதைச் சரிபார்க்கவும், கணக்கீடுகள் முடிவடையும் வரை காத்திருக்கவும், கணக்கீடுகளின் முடிவைப் பெறவும் மற்றும் பலவற்றை இது அனுமதிக்கிறது.
எதிர்கால இடைமுகத்தின் முறைகள்
-
boolean isDone() — இந்த பணி (கணக்கீடு) செய்யப்பட்டால் இந்த முறை உண்மையாக இருக்கும். சாதாரணமாக முடிவடைந்த, விதிவிலக்குடன் முடிந்த அல்லது ரத்துசெய்யப்பட்ட பணிகள் முடிந்ததாகக் கருதப்படும்.
-
V get() — தேவைப்பட்டால், இந்த முறை அதை அழைத்த நூலைத் தடுக்கிறது மற்றும் கணக்கீடுகள் முடிந்ததும் அதன் முடிவை வழங்கும்.
-
வி கெட்(நீண்ட நேரம் முடிந்தது, டைம்யூனிட் யூனிட்) — முந்தைய முறையைப் போலவே, இந்த முறை அதை அழைத்த தொடரை தடுக்கிறது, முடிவுக்காக காத்திருக்கிறது, ஆனால் முறை அளவுருக்கள் மூலம் குறிப்பிடப்பட்ட நேரத்திற்கு மட்டுமே.
-
பூலியன் கேன்சல்(பூலியன் மேஇன்டர்ரப்ட்இஃப்ரன்னிங்) — இந்த முறை பணியை செயல்படுத்துவதை நிறுத்த முயற்சிக்கிறது. பணி இன்னும் இயங்கத் தொடங்கவில்லை என்றால், அது ஒருபோதும் இயங்காது. பணி நடந்து கொண்டிருந்தால், மேஇன்டர்ரப்ட்இஃப்ரன்னிங் அளவுரு, பணியைச் செயல்படுத்தும் த்ரெட்டில் குறுக்கிட முயற்சி செய்யப்படுமா என்பதைத் தீர்மானிக்கிறது. ரத்துசெய்யும் முறை அழைக்கப்பட்ட பிறகு , isDone முறை எப்போதும் உண்மையாகத் திரும்பும் .
-
boolean isCancelled() — சாதாரணமாக முடிவதற்குள் பணி ரத்து செய்யப்பட்டால், இந்த முறை உண்மையாக இருக்கும். ரத்துசெய்யும் முறை முன்பு அழைக்கப்பட்டு உண்மை எனத் திரும்பியிருந்தால், முறை எப்போதும் உண்மையாகத் திரும்பும் .
அழைக்கக்கூடிய மற்றும் எதிர்காலத்தைப் பயன்படுத்தும் குறியீட்டின் எடுத்துக்காட்டு
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.*;
public class Fibonacci implements Callable<Integer> {
private final int index;
public Fibonacci(int index) {
this.index = index;
}
@Override
public Integer call() {
int first = 0;
int second = 1;
if (index == 1) {
return first;
} else if (index == 2) {
return second;
} else {
for (int i = 0; i < index - 2; i++) {
int temp = second;
second += first;
first = temp;
}
return second;
}
}
public static Future<Integer> calculateAsync(int index) throws Exception {
Fibonacci fibonacci = new Fibonacci(index);
// The future object will represent the result of running the fibonacci task.
FutureTask<Integer> future = new FutureTask<>(fibonacci);
// Because the FutureTask class implements both the Future interface and the Runnable interface,
// you can pass instances of it to the Thread constructor
Thread thread = new Thread(future);
thread.start();
return future;
}
}
பின்வரும் முக்கிய முறையை இயக்குவோம் :
public static void main(String[] args) throws Exception {
Map<Integer, Future<Integer>> tasks = new HashMap<>();
for (int i = 10; i < 20; i++) {
tasks.put(i, Fibonacci.calculateAsync(i));
}
for (Map.Entry<Integer, Future<Integer>> entry : tasks.entrySet()) {
Future<Integer> task = entry.getValue();
int index = entry.getKey();
int result;
// Check whether the task is done
if (task.isDone()) {
// Get the result of the calculations
result = task.get();
} else {
try {
// Wait another 100 milliseconds for the result of the calculations
result = task.get(100, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
// Interrupt the task
task.cancel(true);
System.out.println("Fibonacci number " + index + " could not be calculated in the allotted time.");
return;
}
}
System.out.println("Fibonacci number " + index + ": " + result);
}
}
பணியகம் காண்பிக்கும்:
ஃபிபோனச்சி எண் 17: 987
ஃபைபோனச்சி எண் 18: 1597
பிபோனச்சி எண் 19: 2584
பிபோனச்சி எண் 10: 34
பிபோனச்சி எண் 11: 55
பிபோனச்சி எண்
12: 849
233
ஃபைபோனச்சி எண் 15: 377
GO TO FULL VERSION