본문 바로가기
JAVA

230102_Thread

by 경 훈 2023. 1. 2.

program : 명령어를 나열

process : 프로세스 -> CPU 내에서 실행중인 프로그램

스레드(Thread) : 프로세스를 상세하게 구분

가장 작은 단위의 실행되는 프로그램

그러한 기능을 갖는 클래스를 상속 =>

Thread : 클래스  ... Runnable 인터페이스를 상속

Runnable : 인터페이스

동기화 : 임계영역에 들어간 쓰레드가 자기 실행 종료시 동기화

 

package day21_Thread1;
class Sample1 implements Runnable{

	@Override
	public void run() {
		System.out.println("Runable이라는 인터페이스를 상속받았다");
	}
}
public class Thread_Test {

	public static void main(String[] args) {
		Sample1 ss = new Sample1();
		Thread tt = new Thread(ss);
		tt.start();
	}
}

 

package day21_Thread1;
class Sample1 extends Thread{
	public void run() {
		System.out.println("Thread 클래스를 이용했다");
	}
}
public class Thread_Test {
	public static void main(String[] args) {
		Sample1 ss = new Sample1();
		ss.start();
	}
}

 

package day21_Thread1;
class GuguDan extends Thread{
	int dan; //외부에서 단을 넘겨받는다

	public GuguDan(int dan) {
		super();
		this.dan = dan;
	}
	public void run() {
		for(int i=1;i<=9;i++) {
			try {
				Thread.sleep(1000);
                System.out.println(dan+"*"+i+"="+(dan*i));
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
public class Thread_Test {
	public static void main(String[] args) {
		GuguDan g4 = new GuguDan(4);
		g4.start();
		GuguDan g7 = new GuguDan(7);
		g7.start();
		GuguDan g6 = new GuguDan(6);
		g6.start();
	}
}

 

package day21_Thread1;

import java.util.Random;

class GuguDan extends Thread{
	int dan; //외부에서 단을 넘겨받는다

	public GuguDan(int dan) {
		super();
		this.dan = dan;
	}
	public synchronized void run() { //synchronized 동기화
		if(dan%2==1)
		{
			try {
				System.out.println("홀수이므로 5초 대기");
				Thread.sleep(5000);
				}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		for(int i=1;i<=9;i++) {
			try {
				Thread.sleep(1000);
                System.out.println(dan+"*"+i+"="+(dan*i));
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
public class Thread_Test {
	public static void main(String[] args) {
		//임의의 구구단을 생성해서 ?단을 넘겼을때 홀수 구단 이라면 10초 대기상태 슬립
		Random rd =new Random();
		int n = rd.nextInt(9)+1;
		GuguDan gn = new GuguDan(n);
		gn.start();
	}
}

 

package day21_Thread1;
class GuguDan extends Thread{
	int dan; //외부에서 단을 넘겨받는다

	public GuguDan(int dan) {
		super();
		this.dan = dan;
	}
	public void run() {
		if(dan == 5)
		{
			try {
				join(10000);
			}
			catch (InterruptedException e ) {
				e.printStackTrace();
			}
		}
		for(int i=1;i<=9;i++) {
			try {
				Thread.sleep(1000);
                System.out.println(dan+"*"+i+"="+(dan*i));
			}
			catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}
public class Thread_Test {
	public static void main(String[] args) {
		GuguDan g4 = new GuguDan(4);
		g4.start();
		GuguDan g7 = new GuguDan(7);
		g7.start();
		GuguDan g5 = new GuguDan(5);
		g5.start();
	}
}

 

package day21_Thread2;
class koko extends Thread{ //스레드 클래스를 상속받는경우
	public void run() {
		System.out.println("스레드 클래스");
	}
}
class koko1 implements Runnable { //런에이블 인터페이스를 상속받는경우
	@Override
	public void run() {
		System.out.println("인터페이스");
	}
}
public class Sample3 {

	public static void main(String[] args) {
		koko kk = new koko();
		kk.start();
		koko1 kk1 = new koko1();
		Thread tt = new Thread(kk1);
		tt.start();
	}
}

 

package day21_Thread2;
class Ezen1 extends Thread {
	int n;
	public Ezen1(int n) {
		this.n = n;
	}
	public void run() {
		for(int i=1;i<=9;i++) {
			System.out.println(n+"*"+i+"="+(n*i));
		}
	}
}
public class Gudan {
	public static void main(String[] args) {
		Ezen1 ez4 = new Ezen1(4);
		ez4.start();
		Ezen1 ez9 = new Ezen1(9);
		ez9.start();
	}
}

 

package day21_Thread2;

import java.util.Random;

class Ezen3 extends Thread {
	String name;
	int su=0;
	Random rd = new Random(); 
	public Ezen3(String name) {
		super();
		this.name = name;
	}
	@Override
	public void run() {
		su = rd.nextInt(6)+1;
		System.out.println(name+" : " +su);
	}
}
public class Sample4 {
	public static void main(String[] args) {
		Ezen3 tt = new Ezen3("철수");
		Ezen3 tt3 = new Ezen3("영희");
		tt3.setPriority(10);
		tt.start();
		tt3.start();
		try {
			tt3.join();
		}
		catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("철수의 값 : "+tt.su);
		System.out.println("영희의 값 : "+tt3.su);
		String str;
		if(tt.su>tt3.su) str = "철수가 크다";
		else str = "영희가 크다";
		System.out.println(str);
	}
}

 

package day21_Thread2;

import java.util.Random;
class Ezen5 extends Thread {
	String name;
	int su=0;
	int sum=0;
	Random rd = new Random();
	public Ezen5(String name) {
		super();
		this.name = name;
	}
	public void run() {
		for(int i=1;i<=10;i++) {
			su=rd.nextInt(6)+1;
			sum=sum+su;
		}
		System.out.println(name+"의 합 : "+sum);
	}
}
public class Sample5 {
	public static void main(String[] args) {
		Ezen5 ez1 = new Ezen5("철수");
		Ezen5 ez2 = new Ezen5("영희");
		Ezen5 ez3 = new Ezen5("바둑이");
		ez1.start();
		ez2.start();
		ez3.start();
		try {
			ez1.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		String str = null;
		if(ez1.sum>ez2.sum) 
			if(ez1.sum>ez3.sum) 
				str="철수가 제일 높다";
			else
				str="바둑이가 제일 높다";
		else if(ez2.sum>ez3.sum) 
			 str="영희가 제일 높다";
		else
			str="바둑이가 제일 높다";
		System.out.println(str);
		String bb=(ez1.sum>ez2.sum)? (ez1.sum>ez3.sum)? "철수가 제일 높다" : "바둑이가 제일 높다" : (ez2.sum>ez3.sum)? "영희가 제일 높다" : "바둑이가 제일 높다";
		System.out.println(bb);
	}
}

'JAVA' 카테고리의 다른 글

230104_Map  (0) 2023.01.04
230103_Collection  (0) 2023.01.03
221230_SingleTon  (0) 2022.12.30
221229_SingleTon  (0) 2022.12.29
221228_SingleTon  (0) 2022.12.28

댓글