在office实例中,getsum((返回n个数字的总和,并且一次只允许一个线程在睡眠1秒的情况下完成计算。在此期间,其他线程将尝试锁定,如果线程未获得锁定,它将在控制台上打印else条件,并在dowhile循环中迭代,直到完成
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Office {
private ReentrantLock lock = new ReentrantLock(true);
public void login(String name) {
System.out.println("Good Morning : " + name);
}
public long getSum(String name, long number) {
long sum = 0;
try {
do {
if (lock.tryLock(500, TimeUnit.MILLISECONDS)) {
for (int i = 1; i <= number; i++) {
sum = sum + i;
Thread.sleep(1000);
}
lock.unlock();
break;
} else {
System.out
.println("waiting for lock : " + name + " " + Thread.currentThread() + " " + "is waiting");
}
} while (true);
} catch (InterruptedException e) {
e.printStackTrace();
}
return sum;
}
public void logout(String name) {
System.out.println("Good night : " + name);
}
}
import java.util.concurrent.Callable;
public class MyCallable implements Callable {
private Office office;
private String name;
private long number;
public MyCallable(Office office, String name, long number) {
this.office = office;
this.name = name;
this.number = number;
}
@Override
public Object call() throws Exception {
office.login(name);
long total = office.getSum(name, number);
office.logout(name);
return total + " " + name;
}
}
这里future.get((如果我们使用,我们无法通过控制台获取线程等待语句,如果我们不使用,则waiting语句将成功显示在控制台上。我有点好奇为什么get((只在Thread完成时显示结果,并在必要时等待计算完成,然后检索结果。它如何影响办公实例的getSum中的else语句
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) {
long total = 0;
String name = null;
Office office = new Office();
MyCallable myCallable[] = { new MyCallable(office, "Nik", 10), new MyCallable(office, "Dev", 20),
new MyCallable(office, "push", 30), new MyCallable(office, "Sam", 40) };
ExecutorService service = Executors.newFixedThreadPool(2);
for (MyCallable callable : myCallable) {
Future future = service.submit(callable);
try {
String result[] = future.get().toString().split(" ");
total = Integer.parseInt((result[0]));
name = result[1];
System.out.println(name + " " + total);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
service.shutdown();
}
}
我使用future.get((方法在没有任何等待Threadelse语句的情况下获得输出
OUTPUT:
Good Morning : Nik
Good night : Nik
Nik 55
Good Morning : Dev
Good night : Dev
Dev 210
Good Morning : push
Good night : push
push 465
Good Morning : Sam
Good night : Sam
Sam 820
在这里,我期望else语句也用于等待Threads,但如果我使用future.get((,则不会得到它;
您需要先提交所有期货,然后再尝试调用.get()
。以下是您修改的主要方法:
public static void main(String[] args) {
long total = 0;
String name = null;
Office office = new Office();
MyCallable myCallable[] = {
new MyCallable(office, "Nik", 10),
new MyCallable(office, "Dev", 20),
new MyCallable(office, "push", 30),
new MyCallable(office, "Sam", 40) };
ExecutorService service = Executors. newFixedThreadPool(2);
List<Future> futures = new ArrayList<>();
for (MyCallable callable : myCallable) {
futures.add(service.submit(callable));
}
for (Future future : futures) {
try {
String result[] = future.get().toString().split(" ");
total = Integer.parseInt((result[0]));
name = result[1];
System.out.println(name + " " + total);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
service.shutdown();
}
这会产生以下输出:
Good Morning : Dev
Good Morning : push
waiting for lock : Dev Thread[pool-1-thread-2,5,main] is waiting
waiting for lock : Dev Thread[pool-1-thread-2,5,main] is waiting