관리 메뉴

moozi

자바쓰레드 동기화 관련예제 본문

TIS_2016/안드로이드_1기

자바쓰레드 동기화 관련예제

moozi 2016. 4. 25. 17:39

class Process{

//동기화로 처리

public synchronized void process(){

for(int i=0;i<100;i++){

String ThreadName=Thread.currentThread().getName();

System.out.println(ThreadName+":"+i);

}

}

public void process2(){

for(int i=0;i<100;i++){

String ThreadName=Thread.currentThread().getName();

System.out.println("===="+ThreadName+":"+i);

}

}

}


//Runnable이 인터페이스이므로 추가로 다른 클래스 상속 가능.

class myThread implements Runnable{

Process proc;

public myThread(Process p){

this.proc=p;

}

public void run(){

proc.process();

proc.process2();

}

}

public class Thread03 {


public static void main(String[] args) {

//값을 증가시키는 작업을 담당하는 인스턴스 생성

Process proc=new Process();

Thread t =new Thread(new myThread(proc));

t.start();


Thread t2 =new Thread(new myThread(proc));

t2.start();

}


}



'TIS_2016 > 안드로이드_1기' 카테고리의 다른 글

sql연습문제  (0) 2016.04.28
join연습  (0) 2016.04.27
조인연습 테이블  (0) 2016.04.27
4/26 JDBC 프로그래밍 예제  (0) 2016.04.26
자바기본문법  (0) 2016.04.25
Comments