Thread : νλ‘μΈμ€ μμμμ νλμ νλ¦
μλ°λ Multi Thread μ§μ (λκΈ° μ€, λ€λ₯Έ μΌμ μ²λ¦¬νμ¬ μ²λ¦¬ μλ ν₯μ)
Thread ꡬννλ λ²μ λ κ°μ§κ° μλ€.
첫째, Thread classλ₯Ό μμλ°μμ μ¬μ©
λμ§Έ, Runnable classλ₯Ό μμλ°μμ μ¬μ©
μλ°λ λ€μ€ μμμ νμ©νμ§ μμΌλ―λ‘, ν ν΄λμ€λ₯Ό μμλ°μμΌνλ κ²½μ°λλ¬Έμ 2λ²μ μ£Όλ‘ μ¬μ©
Runnableμ μ¬μ©νμ¬ Threadλ₯Ό ꡬνν΄λ³΄μ!
κΈ°λ³Έ ꡬν
package study;
//Runnable interface μμλ°λ λ°©μμΌλ‘ Thread ꡬν (run() method ꡬν νμ)
public class ThreadSample implements Runnable {
private String TEMPLATE = "μΆλ ₯μ€ [%s][%dν]";
private String threadName;
//μμ±μ
public ThreadSample (String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
for(int i=0; i<=100; i++) {
System.out.println(String.format(TEMPLATE, threadName, i));
}
}
public static void main(String[] args) {
ThreadSample run1 = new ThreadSample("thread1");
ThreadSample run2 = new ThreadSample("thread2");
ThreadSample run3 = new ThreadSample("thread3");
Thread thread1 = new Thread(run1);
Thread thread2 = new Thread(run2);
Thread thread3 = new Thread(run3);
thread1.start();
thread2.start();
thread3.start();
}
}
Thread Pool μμ
Threadλ₯Ό μ νλ κ°μλ§νΌ λ§λ€μ΄λκ³ , μμ νμ λ€μ΄μ€λ μμ λ€μ Threadκ° νλμ© λ§‘μμ μ²λ¦¬νλ λ°©μ
package study;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
//Runnable interface μμλ°λ λ°©μμΌλ‘ Thread ꡬν (run() method ꡬν νμ)
//Thread pool μ΄μ©νμ¬ μμ± μ, μ΅λ κ°μλ₯Ό μ ν μ§μ
public class ThreadPoolSample implements Runnable {
private String TEMPLATE = "μΆλ ₯μ€ [%s][%dν]";
private String threadName;
//μμ±μ
public ThreadPoolSample (String threadName) {
this.threadName = threadName;
}
@Override
public void run() {
for(int i=0; i<=100; i++) {
System.out.println(String.format(TEMPLATE, threadName, i));
}
}
public static void main(String[] args) {
ThreadPoolSample run1 = new ThreadPoolSample("thread1");
ThreadPoolSample run2 = new ThreadPoolSample("thread2");
ThreadPoolSample run3 = new ThreadPoolSample("thread3");
//μ§μ Thread μμ±νμ§ μκ³ , Thread pool μμ±νμ¬ κ°μλ₯Ό 3κ°λ‘ μ ννκ³ μ€νν¨
ExecutorService executorService = Executors.newFixedThreadPool(3);
executorService.execute(run1);
executorService.execute(run2);
executorService.execute(run3);
executorService.shutdown();
try {
/**
* awaitTermination()
* ν΄λΉ μκ° λ§νΌ μ νμ λκ³ , κ·Έ μκ° μμ Thread pool μμ
μ΄ μ λΆ μνλμ§ λͺ»νλ©΄
* μ€ν μ€μ΄λ Threadμ μΈν°λ½νΈ λ°μ(.interrupt())νκ³ false λ°ν
* μΈν°λ½νΈλ? μ€λ λκ° μΌμμ μ§ μνμ μμλ, InterruptedException μμΈ λ°μμ νλ μν
* μ΄κ²μ μ΄μ©νλ©΄ μ€λ λμ run() λ©μλλ₯Ό μ μ μ’
λ£ μν¬ μ μμ
*/
if(!executorService.awaitTermination(5, TimeUnit.MINUTES)) {
//shutdown() : μ€ν μ€μΈ μμ
λΏ μλλΌ, μμ
νμ λκΈ° μ€μΈ λͺ¨λ μμ
μ λ€ μ²λ¦¬νκ³ Thread pool μ€μ§
//shutdownNow() : μΈν°λ½νΈλ‘ μ¦μ μ€μ§ν¨!
executorService.shutdownNow();
}
} catch (InterruptedException e) {
e.printStackTrace();
executorService.shutdownNow();
}
}
}
μ κΉ! μ¬κΈ°μ Thread poolμ 3κ°λ‘ μ ννκ³ , Runnable κ°μ²΄λ 4κ°λ₯Ό λ§λ λ€λ©΄ μ½μμ μ΄λ»κ² μ°νκΉ?
μμ κ°μ²΄μΈ Thread1~3μ΄ 100κΉμ§ μ€νλκ³ , μ€νμ΄ λλλ©΄ λ€μ κ°μ²΄μΈ Thread4κ° λλ©° 100κΉμ§ μ°λλ€.
Interrupt ꡬν
μ€λ λλ μμ μ run() λ©μλκ° λͺ¨λ μ€νλλ©΄, μλμΌλ‘ μ’
λ£λλ€. κ·Έλ¬λ μ€λ λλ₯Ό μ¦μ μ’
λ£ν΄μΌ ν μλ μλλ°, μ΄λ° κ²½μ°μ interruptκ° μ°μ¬μ§λ€. (stopμ deprecated λμλ€.)
μ£Όμμ μ! μ€λ λκ° μ€ν λκΈ°λ, μ€ν μνμμ interrupt() methodκ° μ€νλλ©΄ μμΈκ° λ°μνμ§ μλλ€. λ°λμ, μΌμμ μ§ μνκ° μμ΄μΌ InterruptedException μμΈκ° λ°μνλ€. κ·Έλμ sleep methodλ₯Ό μ΄μ©νμ¬ μμ λ₯Ό ꡬνν΄λ³΄μλ€.
package study.thread;
public class InterruptSample {
//1μ΄ ν, μ€λ λ μ€μ§μν΄
public static void main(String[] args) {
Thread thread = new ThreadPrint();
thread.start();
try {
//μΌμμ μ§μ¬μΌλ§ μμΈκ° λ°μνλ―λ‘
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//μΌμμ μ§κ° λμ΄ μμΈκ° λ°μνκ³ , μ€λ λλ₯Ό μ’
λ£μν€κΈ° μν΄ interrupt call
//Thread μ’
λ£ μν΄ InterruptedException λ°μμν΄
thread.interrupt();
}
}
package study.thread;
//μ΄λ²μ Thread classλ₯Ό μμλ°λ λ°©λ²μΌλ‘ ꡬν
public class ThreadPrint extends Thread {
@Override
public void run() {
try {
while(true) {
System.out.println("μ€νμ€");
Thread.sleep(1); //μΌμμ μ§λλ©΄ InterruptedException λ°μ
}
} catch (InterruptedException e) {
System.out.println("InterruptedException μμΈκ° λ°μνμμ΅λλ€");
e.printStackTrace();
}
System.out.println("μ€νμ’
λ£");
}
}
'π Language > Java' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Java] Lombok μ΄λ Έν μ΄μ μ 리 (0) | 2022.07.20 |
---|---|
[Error] org.gradle.tooling.BuildException: Could not fetch model of type 'GradleBuild' using connection to Gradle distribution (0) | 2022.03.15 |
[Java] this, this() (0) | 2022.03.01 |
[Java] μ μΊμ€ν μ νλ μ΄μ (0) | 2022.03.01 |
[Eclipse] Run asμμ Junit Testμ μ νν μ μμ λ? Missing Run as JUnit Test (0) | 2022.02.19 |