new Thread(new Runnable() {
public void run() {
System.out.println("Look ma, no hands");
}
}).start();
new Thread(new Runnable() {
public void run() {
System.out.println("Look at me, look at me...");
}
}).start();
가 잘 작동 사전에
감사합니다 ....
는 는 기본적으로 간단한 작업의 몇 가지를 실행하는 ExecutorService
을 사용하여 ...
그래서 나는이 정말 간단한 예를 쓴 ExecutorService를 예 업데이트되었습니다. 약자로, 두 작업은
지금
ExecutorService service = Executors.newFixedThreadPool(1);
에
ExecutorService service = Executors.newFixedThreadPool(2);
변경 ... 서로 (동시) 병렬로
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(2);
service.submit(new PathScanner());
service.submit(new Counter());
service.shutdown();
service.awaitTermination(1, TimeUnit.DAYS);
System.exit(0);
}
public static class PathScanner implements Callable<Object> {
@Override
public Object call() throws Exception {
scan(new File("C:/"), 0);
return null;
}
protected void scan(File path, int deepth) {
if (deepth < 15) {
System.out.println("Scanning " + path + " at a deepth of " + deepth);
File[] files = path.listFiles();
for (File file : files) {
if (file.isDirectory()) {
scan(file, ++deepth);
}
}
}
}
}
public static class Counter implements Callable<Object> {
@Override
public Object call() throws Exception {
for (int index = 0; index < 1000; index++) {
Thread.sleep(1);
System.out.println(index);
}
return null;
}
}
실행을 실행하고 다시 실행됩니다. 그 차이를 보았 니?
executor가 큐를 처리하는 동안 사용할 수있는 동시 스레드의 수를 제어하는 방법입니다.
더 많은 작업을 만들어 대기열에 추가하고 얻은 것을 확인하십시오.
는 *은 예외 * 여기에 예외를 게시 할 수 유용 할 수 있습니다 발생 – Robin
어쨌든에서 관련 작업이 있습니까? 데이터가 저장된 후에 알림을 보내시겠습니까 ?? 아니면 그냥 대기열에 무작위 작업을 잔뜩 대기열에두고 싶니 ??? – MadProgrammer
@MadProgrammer 귀하의 빠른 회신에 감사드립니다.데이터베이스 두 작업은 서로 독립적입니다. 데이터가 먼저 저장되거나 푸시 알림이 먼저 전송되는지 여부는 중요하지 않습니다. –