일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 웹
- CSS
- javascript
- Java
- web
- Oracle
- 객제지향프로그래밍
- 오라클
- 코딩
- html
- UI
- 자바
- ERWin
- 객체지향
- Project
- 프로젝트
- 프로그래밍
- 웹프로그래밍
- 공부
- 테이블
- jsp
- orcle
- DB
- 객체지향프로그래밍
- 주말이다..
- 객제지향
- squery
- 데이터베이스
- sql
- 공부를열심히
Archives
- Today
- Total
햄찌개
JAVA - 쓰레드 수행시간 체크 본문
쓰레드 수행 시간 체크
public class T03_ThreadTest {
public static void main(String[] args) {
Thread th = new Thread(new MyRunner());
//1970년 1월 1일 0시 0분 0초(표준시)로 부터 경과한 시간을 밀리세컨드(1/1000초)단위로 나타내라.
long startTime = System.currentTimeMillis();
th.start();//쓰레드 시작
try {
th.join(); //현재 실행중인 쓰레드에0서 작업중인 쓰레드(지금은 th쓰레드)가 종료 될 때까지 기다린다.
} catch (InterruptedException e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("경과시간 : "+(endTime-startTime)+"ms" );
}
}
class MyRunner implements Runnable{
//1~10억까지 합계 구하는 메서드
@Override
public void run() {
long sum =0;
for (long i = 0L; i <= 1000000000; i++) {
sum+=i;
}
System.out.println("합계 : " +sum);
}
}
* 1~20억 합계를 구하는데 걸리는 시간체크하기
* 전체 합계를 구하는 작업을 단독으로 처리했을때 (1개의 스레드를 사용했을때)와
* 여러 스레드로 분할해서 작업을 할때의 시간을 확인해보자.
public static void main(String[] args) {
//단독으로 처리할 때
sumThread sm = new sumThread(1L, 2000000000L);
long startTime = System.currentTimeMillis();
sm.start();
try {
sm.join();
} catch (InterruptedException e) {
e.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 (sumThread th : sumThs) {
th.start();
}
for (sumThread th : sumThs) {
try {
th.join();
} catch (Exception e) {
e.printStackTrace();
}
}
endTime = System.currentTimeMillis();
System.out.println("협력으로 처리할 때 처리 시간 "+(endTime-startTime));
System.out.println("\n\n");
}
}
class sumThread extends Thread{
private long max, min;
public sumThread(long min, long max) {
this.max = max;
this.min = min;
}
@Override
public void run() {
long sum=0L;
for (int i = 0; i < max; i++) {
sum+=i;
}
System.out.println(min+" ~ "+max +" 합 : "+sum );
}
}
'고급 JAVA' 카테고리의 다른 글
JAVA - 쓰레드 예제 - 가위바위보 (0) | 2020.09.23 |
---|---|
JAVA - 단일, 멀티 쓰레드 (0) | 2020.09.23 |
JAVA- Thread -싱글,멀티 (0) | 2020.09.21 |
JAVA - Lambda식 (0) | 2020.09.21 |
JAVA - annotation (0) | 2020.09.18 |