상세 컨텐츠

본문 제목

[Java]Thread 예제

JAVA Language

by mobile 2012. 9. 17. 23:29

본문

반응형

- Thread 예제1

class ShowThread extends Thread {

String threadName;

public ShowThread(String name) {

threadName = name;

}

public void run() {

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

{

System.out.println("안녕하세요. " + threadName + "입니다.");

try {

sleep(100);

} catch(InterruptedException e) {

e.printStackTrace();

}

}

}

}


public class ThreadUnderstand {


public static void main(String[] args) {

ShowThread st1 = new ShowThread("멋진 쓰레드");

ShowThread st2 = new ShowThread("예쁜 쓰레드");

st1.start();

st2.start();

}

}


- Thread 예제2

class Sum {

int num;


public Sum() {

num = 0;

}


public void addNum(int n) {

num = num + n;

}


public int getNum() {

return num;

}

}


class AdderThread extends Sum implements Runnable {

int start, end;

public AdderThread(int s, int e){

start = s;

end = e;

}

public void run() {

for(int i=start; i<=end ; i++)

addNum(i);

}

}


public class RunnableThread {


public static void main(String[] args) {

AdderThread at1 = new AdderThread(1,50);

AdderThread at2 = new AdderThread(51,100);

Thread tr1 = new Thread(at1);

Thread tr2 = new Thread(at2);

tr1.start();

tr2.start();

try {

tr1.join();

tr2.join();

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println("1~100까지의 합 : " + (at1.getNum()+ at2.getNum()));

}

}


- Thread 예제3

class MessageSendingThread extends Thread {

String message;

public MessageSendingThread(String str) {

message = str;

}

@Override

public void run() {

for(int i=0; i<1000000; i++)

System.out.println(message + "(" + getPriority() + ")");

}

}


public class PriorityTestOne {


public static void main(String[] args) {

MessageSendingThread tr1 = new MessageSendingThread("First");

MessageSendingThread tr2 = new MessageSendingThread("Second");

MessageSendingThread tr3 = new MessageSendingThread("Third");

tr1.start();

tr2.start();

tr3.start();

}

}


- Thread 예제4

class MessageSendingThread2 extends Thread {

String message;


public MessageSendingThread2(String str, int prio) {

message = str;

setPriority(prio);

}


@Override

public void run() {

// TODO Auto-generated method stub

for(int i=0; i<1000000; i++)

System.out.println(message + "(" + getPriority() + "}");

}

}


public class PriorityTestTwo {


public static void main(String[] args) {

// TODO Auto-generated method stub

MessageSendingThread2 tr1 = new MessageSendingThread2("First", Thread.MAX_PRIORITY);

MessageSendingThread2 tr2 = new MessageSendingThread2("Second", Thread.NORM_PRIORITY);

MessageSendingThread2 tr3 = new MessageSendingThread2("Third", Thread.MIN_PRIORITY);

tr1.start();

tr2.start();

tr3.start();

}

}


- Thread 예제 5

class MessageSendingThread3 extends Thread {

String message;


public MessageSendingThread3(String str, int prio) {

message = str;

setPriority(prio);

}


@Override

public void run() {

// TODO Auto-generated method stub

// super.run();

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

System.out.println(message + "(" + getPriority() + ")");

}

try {

sleep(1);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}


public class PriorityTestThree {


public static void main(String[] args) {

// TODO Auto-generated method stubf

MessageSendingThread3 tr1 = new MessageSendingThread3("First", Thread.MAX_PRIORITY);

MessageSendingThread3 tr2 = new MessageSendingThread3("Second", Thread.NORM_PRIORITY);

MessageSendingThread3 tr3 = new MessageSendingThread3("Third", Thread.MIN_PRIORITY);

tr1.start();

tr2.start();

tr3.start();

}

}


반응형

관련글 더보기

댓글 영역