在此处为办公室实例getsum()返回n个数字的总和,并且只有一个线程允许一次睡眠1秒以完成计算。在此期间,其他线程将尝试锁定,如果线程未获得锁定,它将在控制台上打印其他条件,并在do-while循环中进行迭代,直到完成
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()如果正在使用,则无法通过控制台获取线程等待语句,如果不是,则无法通过控制台成功显示等待语句。我很好奇为什么会发生这种情况,因为get()仅在线程完成时显示结果,并在必要时等待计算完成,然后检索其结果。它如何影响Office实例的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()方法无需等待线程else
即可获得输出 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
Here i'm expecting else statement also for waiting Threads but not getting that if i use 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.newCachedThreadPool();
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
Good Morning : Nik
Good Morning : Sam
waiting for lock : Nik Thread[pool-1-thread-1,5,main] is waiting
waiting for lock : Sam Thread[pool-1-thread-4,5,main] is waiting