Created
March 18, 2014 06:24
-
-
Save sjyun/9614552 to your computer and use it in GitHub Desktop.
thread 사용 임시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.*; | |
import java.util.concurrent.atomic.AtomicInteger; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: jins | |
* Date: 14. 3. 18 | |
* Time: 오후 1:55 | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class Pan1 { | |
public final ConcurrentHashMap<String, String> conMap = new ConcurrentHashMap<String, String>(); | |
public final AtomicInteger retries = new AtomicInteger(); | |
public final Object lock = new Object(); | |
public static void main(String ar[]) throws InterruptedException, ExecutionException{ | |
int count = 0; | |
List<Runnable> myTasks = new ArrayList<Runnable>(); | |
myTasks.add(getRunnable(1)); | |
myTasks.add(getRunnable(2)); | |
ExecutorService threadPoolService = Executors.newFixedThreadPool(10); | |
CompletionService<Runnable> ecs = new ExecutorCompletionService<Runnable>(threadPoolService); | |
for (Runnable task : myTasks){ | |
ecs.submit(task, task); | |
} | |
while( count++ < 3){ | |
Runnable task = ecs.take().get(); | |
ecs.submit(task, task); | |
} | |
threadPoolService.shutdown(); | |
} | |
private static Runnable getRunnable(final int i){ | |
return new Runnable() { | |
@Override | |
public void run() { | |
System.out.println("task" + i + "submit" + Thread.currentThread().getName() + ""); | |
try{ | |
Thread.sleep( 500 * i ); | |
}catch( InterruptedException ex){ | |
System.out.println("interrupted"); | |
} | |
System.out.println("Task" + i + "complteted" + Thread.currentThread().getName() + ""); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment