동시에 여러 작업을 할 수 있는 SW
ex) 녹화를 하면서 인텔리 J로 코딩을 하고 워드에 적어둔 문서를 보거나 수정할 수 있다.
멀티프로세싱 (ProcessBuilder)
멀티쓰레드
public static void main(String[] args) {
HelloThread helloThread = new HelloThread();
helloThread.start();
System.out.println("hello : " + Thread.currentThread().getName());
}
static class HelloThread extends Thread {
@Override
public void run() {
System.out.println("world : " + Thread.currentThread().getName());
}
}
Thread thread = new Thread(() -> System.out.println("world : " + Thread.currentThread().getName()));
thread.start();
System.out.println("hello : " + Thread.currentThread().getName());
현재 쓰레드 멈춰두기 (sleep)
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("world : " + Thread.currentThread().getName());
});
thread.start();
System.out.println("hello : " + Thread.currentThread().getName());
}
다른 쓰레드 깨우기 (interupt)
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (true) {
System.out.println("Tread : " + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println("interrupted!");
}
}
});
thread.start();
System.out.println("hello : " + Thread.currentThread().getName());
Thread.sleep(3000);
thread.interrupt();
}
다른 쓰레드 기다리기 (join)
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
System.out.println("Tread : " + Thread.currentThread().getName());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
});
thread.start();
System.out.println("hello : " + Thread.currentThread().getName());
thread.join(); // 해당 쓰레드를 기다린 후
System.out.println(thread + " is finished");
}