24: JAVA THREAD

jk·2024년 2월 1일
0

kdt 풀스택

목록 보기
45/127



1. 단일 프로세스와 멀티 프로세스에 대하여 설명하시오.

  • Single process : The os runs only one process. So other tasks are delayed when something is running.
  • Multi process : The os runs multiple processes at the same time. So it can progress different tasks at the same time.



2. 프로세스와 쓰레드의 차이점은?

  • Process is able to contain threads.
  • Interactions between processes are hard relatively.
  • Interactions between threads ard easy relatively.



3.아래의 가로 찍기와 세로찍기를 쓰레드로 돌리시오.

		for(int i=0; i < 300; i++)
			System.out.printf("%s", new String("-"));		
//
//		
		for(int i=0; i < 300; i++) 
			System.out.printf("%s", new String("|"));
//
//code
//
class Functions {
    private static Thread threadHyphen = new ThreadHyphen();
    private static Thread threadVerticalBar = new ThreadVerticalBar();
    static void run() {
        while(true) {
            try {
                threadHyphen.start();
                threadVerticalBar.start();
                break;
            } catch(Exception e) {
                e.printStackTrace();
            };
        };
    }
}
class ThreadHyphen extends Thread {
    ThreadHyphen() {
        super();
    }
    @Override
    public void run() {
        for(int i=0; i < 300; i++) {
            System.out.printf("%s", new String("-"));		
        }
    }
}
class ThreadVerticalBar extends Thread {
    ThreadVerticalBar() {
        super();
    }
    @Override
    public void run() {
        for(int i=0; i < 300; i++) {
            System.out.printf("%s", new String("|"));
        };
    }
}
class RunningTwoThreadMain {
    public static void main(String[] args) {
        Functions.run();
    }
}
/*print
-----------------||||||||||||||||||||||||||||---------------------------------------------------------------------------------------------||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||-----------------------------------------------------------------------------------------------------------------------------------------------------------------|||||||||||||||||||||||||||||||||||||||||||||||||||||||||-----------------------------|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/



4. 아래의 의미를 설명해 보시오.

메인 스레드가 종료되더라도 실행 중인 스레드가 하나라도 있다면 프로세스는 종료되지 않습니다.
  • Process is bunch of threads.
  • Main thread is just entry point. Another thread is part of the process too.
  • So the process doesnt end when a thread is running.



5.아래가 돌아 가도록 구현하시오.

main(){
//
    ThreadCount threadCount = new ThreadCount();
//
    threadCount.start();
//
//
//
    String input = JOptionPane.showInputDialog("아무 값이나 입력하세요."); 
//
    System.out.println("입력하신 값은 " + input + "입니다.");
//
}
//
//=============================================
//
//10 9 8 7 6 ... 이 1초마다 실행 되도록 쓰레드를 완성하시오.
//
//code
//
import javax.swing.JOptionPane;
class Const {
    static final int NUMBER_MAX = 10;
    static final int NUMBER_MIN = 0;
}
class Print {
    private static StringBuilder print = new StringBuilder();
    private static void printlnAndReset() {
        System.out.println(print);
        print.setLength(0);
    }
    static <T> void println(T t) {
        print.append(t);
        printlnAndReset();
    }
}
class ThreadCount extends Thread {
    ThreadCount() {
        super();
    }
    @Override
    public void run() {
        while(true) {
            try {
                countDown();
                break;
            } catch(Exception e) {
                e.printStackTrace();
            };
        };
    }
    private void countDown() {
        for(int i = Const.NUMBER_MAX; i > Const.NUMBER_MIN; i--) {
            Print.println(i);
            try {
             Thread.sleep(1000);
            } catch(InterruptedException e) {
                e.printStackTrace();
            };
        };
    }
}
/*print
10
9
8
7
입력하신 값은 jk입니다.
6
5
4
3
2
1
*/



6. 쓰레드 동기화란?

  • Thread synchronization makes only one thread can access the synchronized method at the same time. Another thread can access the method when the other threads dont use the method.
  • It prevents critical section problems.
profile
Brave but clumsy

0개의 댓글