Created
June 4, 2018 07:16
-
-
Save developer-sdk/9b8f21556c0059de64b3fc9842403d0e to your computer and use it in GitHub Desktop.
java thread, ExecutorSerivce example
This file contains hidden or 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
| /** | |
| * Runnable을 상속한 스레드 예제 | |
| * | |
| * @author User | |
| * | |
| */ | |
| public class ThreadExample1 implements Runnable { | |
| @Override | |
| public void run() { | |
| int count = 0; | |
| do { | |
| System.out.printf("%s %d\n", Thread.currentThread().getName(), count++); | |
| } while(count < 10); | |
| } | |
| public static void main(String[] args) { | |
| new Thread(new ThreadExample1()).start(); | |
| new Thread(new ThreadExample1()).start(); | |
| } | |
| } |
This file contains hidden or 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
| /** | |
| * Thread를 상속한 예제 | |
| * | |
| * @author User | |
| * | |
| */ | |
| public class ThreadExample2 extends Thread { | |
| @Override | |
| public void run() { | |
| int count = 0; | |
| do { | |
| System.out.printf("%s %d\n", Thread.currentThread().getName(), count++); | |
| } while(count < 10); | |
| } | |
| public static void main(String[] args) { | |
| new Thread(new ThreadExample2()).start(); | |
| new Thread(new ThreadExample2()).start(); | |
| } | |
| } |
This file contains hidden or 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
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| /** | |
| * 스레드풀 처리 예제 | |
| * Runnable 을 이용하여 결과값을 반환받지 않는 예제 | |
| * | |
| * @author User | |
| * | |
| */ | |
| public class ThreadExample3 extends Thread { | |
| @Override | |
| public void run() { | |
| int count = 0; | |
| do { | |
| System.out.printf("%s %d\n", Thread.currentThread().getName(), count++); | |
| } while (count < 10); | |
| } | |
| public static void main(String[] args) { | |
| ExecutorService service = Executors.newCachedThreadPool(); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.execute(new ThreadExample3()); | |
| service.shutdown(); | |
| } | |
| } |
This file contains hidden or 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
| import java.util.concurrent.Callable; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.Future; | |
| /** | |
| * 스레드풀 처리 예제 | |
| * | |
| * @author User | |
| * | |
| */ | |
| public class ThreadExample4 { | |
| public static void main(String[] args) throws InterruptedException, ExecutionException { | |
| // ExecutorService service = Executors.newCachedThreadPool(); | |
| ExecutorService service = Executors.newFixedThreadPool(100); | |
| Future<Integer> future1 = service.submit(new CallableSample()); | |
| Future<Integer> future2 = service.submit(new CallableSample()); | |
| Future<Integer> future3 = service.submit(new CallableSample()); | |
| System.out.println(future1.get()); | |
| System.out.println(future2.get()); | |
| System.out.println(future3.get()); | |
| service.shutdown(); | |
| } | |
| } | |
| class CallableSample implements Callable<Integer> { | |
| public CallableSample() { | |
| System.out.println("Callable construct"); | |
| } | |
| @Override | |
| public Integer call() throws Exception { | |
| System.out.println("call"); | |
| int result = (int) (Math.random() * 10) + 1; | |
| Thread.sleep(result*1000); | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment