1. Thread Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class ThreadSimple { public static void main(String[] args) { new Thread() { @Override public void run() { for( int i=0; i < 4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } }.start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 0 Thread: 1 Thread: 2 Thread: 3
2. Thread + join Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class ThreadSimpleJoin { public static void main(String[] args) { Thread thread = new Thread() { @Override public void run() { for( int i=0; i < 4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } }; thread.start(); try { thread.join(); } catch (InterruptedException e) { } System.out.println("Main Exit"); } } |
Result:
Thread: 0 Thread: 1 Thread: 2 Thread: 3 Main Exit
3. Thread + Runnable Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class ThreadRunnable { public static void main(String[] args) { new Thread( new Runnable() { @Override public void run() { for( int i=0; i < 4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } }).start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 0 Thread: 1 Thread: 2 Thread: 3
4. Thread + Runnable Lambda Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class ThreadRunnableLambda { public static void main(String[] args) { new Thread( () -> { for( int i=0; i < 4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } }).start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 0 Thread: 1 Thread: 2 Thread: 3
5. Thread Class Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ThreadSimpleClass extends Thread { int start_index; public ThreadSimpleClass(int i) { start_index = i; } @Override public void run() { for( int i=start_index; i < start_index+4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } public static void main(String[] args) { Thread thread = new ThreadSimpleClass(10); thread.start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 10 Thread: 11 Thread: 12 Thread: 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class ThreadRunnableClass implements Runnable { int start_index; public ThreadRunnableClass(int i) { start_index = i; } @Override public void run() { for( int i=start_index; i < start_index+4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } public static void main(String[] args) { new Thread( new ThreadRunnableClass(10) ).start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 10 Thread: 11 Thread: 12 Thread: 13
7. Thread + Runnable Class Multiple Run Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class ThreadRunnableClassMultipleRun implements Runnable { int start_index; public ThreadRunnableClassMultipleRun(int i) { start_index = i; } @Override public void run() { for( int i=start_index; i < start_index+4; i++ ) { try { Thread.sleep(1000); System.out.println("Thread: " + i); } catch (InterruptedException e) { } } } public static void main(String[] args) { Runnable runnable = new ThreadRunnableClassMultipleRun(10); new Thread( runnable ).start(); new Thread( runnable ).start(); System.out.println("Main Exit ..."); } } |
Result:
Main Exit ... Thread: 10 Thread: 10 Thread: 11 Thread: 11 Thread: 12 Thread: 12 Thread: 13 Thread: 13
댓글 없음:
댓글 쓰기