为什么其他条件不显示在等待线程的控制台上。如果我使用 future.get() 方法?
Why else condition not shows over console for waiting Thread. if i'm using future.get() method?
这里是office实例getsum()returnsn位数字之和,一次只允许一个线程完成计算,休眠1秒.在此期间,其他线程将尝试锁定,如果线程未获得锁定,它会在控制台上打印 else 条件并在 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() 仅在 Thread 完成时显示结果,并在必要时等待计算完成,然后检索其结果。它如何影响 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() 方法
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 语句也用于等待线程,但如果我使用 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
这里是office实例getsum()returnsn位数字之和,一次只允许一个线程完成计算,休眠1秒.在此期间,其他线程将尝试锁定,如果线程未获得锁定,它会在控制台上打印 else 条件并在 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() 仅在 Thread 完成时显示结果,并在必要时等待计算完成,然后检索其结果。它如何影响 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() 方法
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 语句也用于等待线程,但如果我使用 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