1. 스레드란 수행중인 프로그램 (프로세스) 안에 존재하는 독립적인 처리단위이다.
2. java.lang.Thread와 java.lang.Runnable을 이용하여 새로운 스레드를 정의하고 인스턴스를 만들어서 수행시키는
코드를 작성한다.
3. Synchronize제한자 및 wait(), notify(), notifyall()메서드를 이용하여 동시접근 문제를 방지하고, 스레드간의 의사
소통을 가능하게 하는 코드를 작성한다.
4. 스레드를 사용하여 프로그램을 작성하는 방법
a. Thread()나 Runnable을 상속받고
b. 스레드안에서 run()메서드 수행할 내용을 오버로딩 하고
c. 이 Thread클래스의 객체를 생성하고 start()메서드를 이용하여 run()메서드를 호출하면 된다.
5. 우선순위(PRIORITY)
최우선 순위 ==> setPriority(Thread.MAX_PRIORITY), setPriority(10)
중간순위 ==> setPrioirty(Thread.NROM_PRIORITY), setPriority(5)
최하위 순위 ==> setPriority(Thread.MIN_PRIORITY), setPriority(1)
-- 그림==-
/*
Thread 클래스 상속 예제
*/
class MyThread extends Thread
{
public void run()// Overriding
{
for(char ch = 'A' ; ch<='Z' ; ch++)
{
System.out.print(" "+ch);
}
}
}
//=------------------------------------------------
class MyThread_2 extends Thread
{
public void run()// Overriding
{
for(char ch = 'a'; ch<='z' ; ch++)
{
System.out.print(" "+ch);
}
}
}
//=------------------------------------------------
class ThreadTest_1
{
public static void main( String [] args )
{
Thread t1 = new MyThread();
Thread t2 = new MyThread_2();
t1.setPriority(10); //우선순위 설정 10>>>>1
//t2.setPriority(1);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}// end main
}
/*
Runnable 인터페이스 상속 예제
*/
class MyThread implements Runnable
{
public void run()
{
for (int i=1 ; i<=10 ; i++)
{
System.out.print(" Good ");
}
}
}
//=------------------------------------------------
class MyThread1 implements Runnable
{
public void run()
{
for(int i=1; i<=10; i++)
{
System.out.print(" Very ");
}
}
}
//=------------------------------------------------
class ThreadTest_2
{
public static void main( String [] args )
{
Thread t1 = new Thread( new MyThread());
Thread t2 = new Thread( new MyThread1());
t1.start();
t2.start();
}// end main
}
class MyThread5 extends Thread
{
int i = 0;
public void run()
{
try
{
while(i<50)
{
i++;
Thread.sleep(1000); //sleep는 예외처리 해야됨.
System.out.println("안녕" + i);
}
}
catch(InterruptedException e) //누가 세치기 하면..
{
}
}
}
class ThreadTest_3
{
public static void main( String [] args )
{
Thread t = new MyThread5();
t.start();
}// end main
}