ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Thread 처리 시간-22.03.30
    고급자바 2022. 3. 30. 11:18
    728x90

    스레드의 수행시간 체크해 보기

    public class T03_ThreadTest {
    	public static void main(String[] args) {
    		// 스레드의 수행시간 체크해 보기
    		
    				Thread th = new Thread(new MyRunner());
    				
    				/*
    				   UTC(Universal Time Coordinated 협정 세계 표준시)를 사용하여 1970년 1월 1일
    				   0시 0분 0초를 기준으로 경과한 시간을 밀리세컨드 단위로 나타낸다. 
    				*/
    				long startTime = System.currentTimeMillis();
    				
    				th.start(); // 스레드 작업 시작
    				
    				try {
    					th.join();  // 현재 실행 중인 스레드에서 작업중인 스레드(지금은 th스레드)가 종료될 때까지 기다린다. run()종료되는 시점에 기다림이풀림
    				} catch (InterruptedException e) {
    					e.printStackTrace();
    				}
    				
    				long endTime = System.currentTimeMillis();
    				
    				System.out.println("경과시간 : " + (endTime - startTime));
    			}
    		}

    1~1000000000 까지의 합계를 구하는 스레드 클래스

    class MyRunner implements Runnable {
    
    			@Override
    			public void run() {
    				long sum = 0;
    				for(int i = 0; i<=1000000000; i++) {
    					sum += i;
    				}
    				System.out.println("합계 : " + sum);
    			}
    		}


    1~20억 까지의 합계를 구하는데 걸린 시간 체크하기
    전체 합계를 구하는 작업을 단독으로 했을 때(1개의 스레드를 사용했을 때)와
    여러 스레드로 분할해서 작업할 때의 시간을 확인해 보자

    단독으로 처리했을 때(시간처리단위: 밀리세컨드)

    public class T04_ThreadTest {
    	public static void main(String[] args) {
    	
    		// 단독으로 처리했을 때(단위: 밀리세컨드)
    				SumThread sm = new SumThread(1L, 2000000000L);
    				
    				long startTime = System.currentTimeMillis();
    				
    				sm.start();
    				
    				try {
    					sm.join();
    				}catch(InterruptedException ex) {
    					ex.printStackTrace();
    				}
    				
    				long endTime = System.currentTimeMillis();
    				
    				System.out.println("단독으로 처리할 때의 처리 시간 : " + (endTime - startTime));
    				System.out.println("\n\n");

    여러 스레드가 협력해서 처리 할때

    SumThread[] sumThs = new SumThread[] {
    					new SumThread(1L,            500000000L),	
    					new SumThread( 500000001L,  1000000000L),	
    					new SumThread(1000000001L,  1500000000L),	
    					new SumThread(1500000001L,  2000000000L)	
    				};
    				
    				startTime = System.currentTimeMillis();
    				
    				for(int i=0; i<sumThs.length; i++) {
    					sumThs[i].start();
    				}
    				
    				for(SumThread th : sumThs) {
    					try {
    						th.join();
    					} catch (InterruptedException e) {
    						e.printStackTrace();
    					}
    				}
    				
    				endTime = System.currentTimeMillis();
    				
    				System.out.println("협력해서 처리 했을때의 처리 시간 : " 
    									+ (endTime - startTime));
    			}
    		}

    더해주는 Thread클래스

    class SumThread extends Thread {
    			private long max, min;
    			
    			public SumThread(long min, long max) {
    				this.min = min;
    				this.max = max;
    			}
    			
    			@Override
    			public void run() {
    				long sum = 0L;
    				for(long i = min; i <= max; i++) {
    					sum += i;
    				}
    				System.out.println(min + " ~ " + max + "까지의 합 : " + sum);
    			}
    		}


    코어 개수에 따라 멀티스레드 활용할 수 있다.

    CPU가 한개라면 나눠서 하는것이 비 효율적일 수도 있음 

    EX) ①은 비효율적 ②오히려 얘가 더 빠를 수도 있음

    728x90

    '고급자바' 카테고리의 다른 글

    ThreadPriority, Daemon Thread-22.03.30  (0) 2022.03.30
    단일, 멀티 Thread-22.03.30  (0) 2022.03.30
    ThreadTest-22.03.29  (0) 2022.03.29
    Annotation,Reflection-22.03.29  (0) 2022.03.29
    가변형 인수,enum-22.03.29  (0) 2022.03.29
Designed by Tistory.