1. 삽입

2 검색

3 삭제 

4 출력

 

linked list 사용

list, set, map버전

package Thread;


public class ThreadExam extends Thread{
	private int [] arr;
	public ThreadExam() {
		arr = new int[10];
		for(int i= 0; i<arr.length; i++) {
			arr[i] = i;
		}
	}
	
	//	실제 동작하는 함수 run
	public void run() {
		for(int i =0;i <arr.length; i++) {
			try {
				sleep(1000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			
			System.out.println(currentThread() + " " +arr[i]);
			
		}
	}
	public static void main(String[] args) {
		ThreadExam te = new ThreadExam();	//스레드 생성함
		te.start();	//	스케줄러가 보다가 동작해 : runable상 태
	}
}

implements Runnable

 

package Thread;

//run 오버라이드 해야한다.
public class ThreadExam implements Runnable{
	private int [] arr;
	public ThreadExam() {
		arr = new int[10];
		for(int i= 0; i<arr.length; i++) {
			arr[i] = i;
		}
	}
	
	public static void main(String[] args) {
		ThreadExam te = new ThreadExam();	//스레드 생성함(쓸 수 있는 환경 만듦)
//		te.start();	//	스케줄러가 보다가 동작해 : runable상 태
		
		Thread th = new Thread(te);	//	동적 바인딩 runnable가능
		th.start();
	}

	@Override
	public void run() {
		for(int i =0;i <arr.length; i++) {
			try {
				Thread.sleep(1000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			
			System.out.println(Thread.currentThread() + " " +arr[i]);
			
		}
		
	}
}

Runable가 보면 run이 abstract로 되어있어서

run( )함수는 무조건 구현해야한다.

 

 

동기화 문제 

package Thread;

class ATM implements Runnable{
	private long depositeMoney = 10000;
	@Override
	public void run() {
		for(int i = 0; i<10;i++) {
			try {
				Thread.sleep(1000);
			}catch(InterruptedException e) {
				e.printStackTrace();
			}
			if(getDespositeMoney() <= 0)
				break;
			withDraw(1000);
		}
	}
	public void withDraw(long howMuch) {
		System.out.println(Thread.currentThread().getName()+ ", ");
		if(getDespositeMoney() > 0) {
			depositeMoney -= howMuch;
			System.out.printf("잔액 : %d원 %s\n",depositeMoney,getDespositeMoney());
		}
		else {System.out.println("잔액이 부족합니다.");}
	}
	
	
	public long getDespositeMoney() {
		return depositeMoney;
	}
}
public class SyncroniazedEx {
	public static void main(String[] args) {
		ATM atm = new ATM();
		Thread trd_mom = new Thread(atm, "mom");
		Thread trd_son = new Thread(atm, "son");
		trd_mom.start();
		trd_son.start();
	}
}

 

동기화 블록

package Thread;

class ATM implements Runnable{
	private long depositeMoney = 10000;
	@Override
	public void run() {
		synchronized (this) {
			for(int i = 0; i<10;i++) {
				try {
					Thread.sleep(1000);
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				if(getDespositeMoney() <= 0)
					break;
				withDraw(1000);
			}
		}
	}
	public void withDraw(long howMuch) {
		System.out.println(Thread.currentThread().getName()+ ", ");
		if(getDespositeMoney() > 0) {
			depositeMoney -= howMuch;
			System.out.printf("잔액 : %d원 %s\n",depositeMoney,getDespositeMoney());
		}
		else {System.out.println("잔액이 부족합니다.");}
	}
	
	
	public long getDespositeMoney() {
		return depositeMoney;
	}
}
public class SyncroniazedEx {
	public static void main(String[] args) {
		ATM atm = new ATM();
		Thread trd_mom = new Thread(atm, "mom");
		Thread trd_son = new Thread(atm, "son");
		trd_mom.start();
		trd_son.start();
	}
}
mom, 
잔액 : 9000원 9000
mom, 
잔액 : 8000원 8000
mom, 
잔액 : 7000원 7000
mom, 
잔액 : 6000원 6000
mom, 
잔액 : 5000원 5000
mom, 
잔액 : 4000원 4000
mom, 
잔액 : 3000원 3000
mom, 
잔액 : 2000원 2000
mom, 
잔액 : 1000원 1000
mom, 
잔액 : 0원 0

 

 

먼저 선점한 스레드가 락 걸어서

다른 스레드 못 씀

기아상태 : 아들이 멈춰져 있음 (비동기 프로세스)

-> 공정하게 써야 함 : 동기화 블록 내에 wait(),notify(), notifyall() ...쓴다.

package Thread;

class ATM implements Runnable{
	private long depositeMoney = 10000;
	@Override
	public void run() {
		synchronized (this) {
			for(int i = 0; i<10;i++) {
				notify();
				try {
					wait();
				}catch(InterruptedException e) {
					e.printStackTrace();
				}
				if(getDespositeMoney() <= 0)
					break;
				withDraw(1000);
			}
		}
	}
	
	public void withDraw(long howMuch) {
		System.out.println(Thread.currentThread().getName()+ ", ");
		if(getDespositeMoney() > 0) {
			depositeMoney -= howMuch;
			System.out.printf("잔액 : %d원 %s\n",depositeMoney,getDespositeMoney());
		}
		else {System.out.println("잔액이 부족합니다.");}
	}
	
	
	public long getDespositeMoney() {
		return depositeMoney;
	}
}
public class SyncroniazedEx {
	public static void main(String[] args) {
		ATM atm = new ATM();
		Thread trd_mom = new Thread(atm, "mom");
		Thread trd_son = new Thread(atm, "son");
		
		trd_mom.start();
		trd_son.start();
	}
}

 

+ Recent posts