CreateAndStartThread_M2C1.java
package thread;
class SMIFileRunnable implements Runnable {
@Override
public void run() {
String[] strArray = {"하나","둘","셋","넷","다섯"};
try {
Thread.sleep(10);} catch (InterruptedException e) {}
for (int i = 0; i < strArray.length; i++) {
System.out.println(" - (자막 번호) " + strArray[i]);
try {Thread.sleep(200);} catch (InterruptedException e) {}
}
}
}
public class CreateAndStartThread_M2C1 {
public static void main(String[] args) {
Runnable smiFileRunnable = new SMIFileRunnable();
Thread thread = new Thread(smiFileRunnable);
thread.start();
int[] intArray = {1, 2, 3, 4, 5};
for (int i = 0; i < intArray.length; i++) {
System.out.print("(비디오 프레임) " + intArray[i]);
try {Thread.sleep(200);} catch (InterruptedException e) {}
}
}
}
CreateAndStartThread_M2C2.java
package thread;
class MIFileRunnable implements Runnable {
@Override
public void run() {
String[] strArray = {"하나", "둘", "셋", "넷", "다섯"};
try {Thread.sleep(10);} catch (interruptedException e) {}
for (int i = 0; i < strArray.length; i++) {
System.out.println(" - (자막 번호) " + strArray[i]);
try {Thread.sleep(200);} catch (InterruptedException e) {}
}
}
}
class VideoFileRunnable implements Runnable {
@Override
public void run() {
int[] intArray = {1,2,3,4,5};
for (int i = 0; i < intArray.length; i++) {
System.out.print("(비디오 프레임)" + intArray[i]);
try {Thread.sleep(200);} catch (InterruptedException e) {}
}
}
}
public class CreateAndStartThread_M2C2 {
public static void main(String[] args) {
Runnable smiFileRunnable = new SMIFileRunnable();
Thread thread1 = new Thread(smiFileRunnable);
thread1.start();
Runnable videoFileRunnable = new VideoFileRunnable();
Thread thread2 = new Thread(videoFileRunnable);
thread2.start();
}
}
package thread;
import javax.swing.plaf.synth.SynthOptionPaneUI;
public class ThreadProperties_1 {
public static void main(String[] args) {
Thread curThread = Thread.currentThread();
System.out.println("현재 쓰레드의 이름 = " + curThread.getName());
System.out.println("동작하는 쓰레드의 개수 = " + Thread.activeCount());
for (int i = 0; i < 3; i++) {
Thread thread = Thread();
System.out.println(thread.getName());
thread.start();
}
for (int i = 0; i < 3; i++) {
Thread thread = new Thread();
thread.setName(i + "번째 쓰레드");
System.out.println(thread.getName());
thread.start();
}
for(int i = 0; i < 3; i++) {
Thread thread = new Thread();
System.out.println(thread.getName());
thread.start();
}
System.out.println("동작하는 쓰레스의 개수 = " + Thread.activeCount());
}
}
ThreadProperties_2.java
package thread;
class MyThread extends Thread {
@Override
public void run() {
for(long i = 0; i < 2_000_000_000_000_000_000L; i++) {} // 시간 지연용
System.out.println(getName() + " 우선순위: " + getPriority());
}
}
public class ThreadProperties_2 {
public static void main(String[] args) {
// CPU 코어 수
System.out.println("코어 수: " + Runtime.getRuntime().availableProcessors());
// 우선순위 자동 지정
for(int i = 0; i < 3; i++) {
Thread thread = new MyThread();
thread.start();
}
try {Thread.sleep(1000);} catch (InterruptedException e) {}
// 우선순위 직접 지정
for(int i = 0; i < 10; i++) {
Thread thread = new MyThread();
thread.setName(i + "번째 쓰레드");
if(i == 9) thread.setPriority(10);
thread.start();
}
}
}
ThreadProperties_3_1.java
package thread;
class MyThread extends Thread {
@Override
public void run() {
System.out.println(getName() + ": " + (isDaemon()? "데몬 쓰레":"일반 쓰레"));
for(int i = 0; i < 6; i++) {
System.out.println(getName() + ": " + i + "초");
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
}
public class ThreadProperties_3_1 {
public static void main(String[] args) {
// 일반 쓰레드
Thread thread1 = new MyThread();
thread1.setDaemon(false);
thread1.setName("thread1");
thread1.start();
// 3.5초 후 main 쓰레드 종료
try {Thread.sleep(3500);} catch (InterruptedException e) {}
System.out.println("main Thread 종");
}
}
ThreadProperties_3_2.java
package thread;
class MyThread_2 extends Thread {
@Override
public void run() {
System.out.println(getName() + ": " + (isDaemon()? "데몬 쓰레드":"일반 쓰레드"));
for(int i = 0; i < 6; i++) {
System.out.println(getName() + ": " + i + "초");
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
}
public class ThreadProperties_3_2 {
public static void main(String[] args) {
// 데몬 쓰레드
Thread thread2 = new MyThread_2();
thread2.setDaemon(true);
thread2.setName("thread2");
thread2.start();
try {Thread.sleep(3500);} catch (InterruptedException e) {}
System.out.println("main Thread 종료");
}
}
접근 지정자 synchronized 리턴 타입 메서드명(입력매개변수) {
// 동기화가 필요한 코드
}
SynchronizedMethod.java
package thread;
// 공유 객체
class MyData {
int data = 3;
public synchronized void plusData() {
int mydata = data;
try {Thread.sleep(2000);} catch (InterruptedException e) {}
data = mydata + 1;
}
}
// 공유 객체를 사용하는쓰레드
class PlusThread extends Thread {
MyData myData;
public PlusThread(MyData myData) {
this.myData = myData;
}
@Override
public void run() {
myData.plusData();
System.out.println(getName() + "실행결과: " + myData.data);
}
}
public class SynchronizedMethod {
public static void main(String[] args) {
// 공유 객체 생성
MyData myData = new MyData();
// plusThread1
Thread plusThread1 = new PlusThread(myData);
plusThread1.setName("plusThread1");
plusThread1.start();
try {Thread.sleep(1000);} catch (InterruptedException e) {}
// plusThread 2
Thread plusThread2 = new PlusThread(myData);
plusThread2.setName("plusThread2");
plusThread2.start();
}
}
KeyObject_2.java
package thread;
class MyData_2{
synchronized void abc() {
for(int i = 0; i < 3; i++) {
System.out.println(i + "sec");
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
synchronized void bcd() {
for (int i = 0; i < 3; i++) {
System.out.println(i + "초");
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
void cde() {
synchronized(new Object()) {
for(int i = 0; i < 3; i++) {
System.out.println(i + "번째");
try {Thread.sleep(1000);} catch (InterruptedException e) {}
}
}
}
}
public class KeyObject_2 {
public static void main(String[] args) {
// 공유 객체
MyData_2 myData = new MyData_2();
// 3개의 쓰레드가 각각의 메서드 호출
new Thread() {
public void run() {
myData.abc();
};
}.start();
new Thread() {
public void run() {
myData.bcd();
};
}.start();
new Thread() {
public void run() {
myData.cde();
};
}.start();
}
}
Thread.State getState()
package thread;
class MyThread_3 extends Thread {
boolean yieldFlag;
@Override
public void run() {
while(true) {
if(yieldFlag) {
Thread.yield();
} else {
System.out.println(getName() + " 실행");
for(long i = 0; i < 100000000L; i++) {} // 시간 지연
}
}
}
}
public class YieldInRunnableState {
public static void main(String[] args) {
MyThread_3 thread1 = new MyThread_3();
thread1.setName("thread1");
thread1.yieldFlag = false;
thread1.setDaemon(true);
thread1.start();
MyThread_3 thread2 = new MyThread_3();
thread2.setName("thread2");
thread2.yieldFlag = true;
thread2.setDaemon(true);
thread2.start();
// 6초 지연(1초마다 한 번씩 양보)
for(int i = 0; i < 6; i++) {
try {Thread.sleep(1000);} catch (InterruptedException e) {}
thread1.yieldFlag = !thread1.yieldFlag;
thread2.yieldFlag = !thread2.yieldFlag;
}
}
}
package thread;
class MyThread extends Thread {
@Override
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
System.out.println(" -- sleep() 진행 중 interrupt() 발생");
for(long i = 0; i < 1000000000L; i++) {} // 시간 지
}
}
}
public class TimeWaiting_Sleep {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
try {Thread.sleep(100);} catch (InterruptedException e) {}
System.out.println("MyThread State = " + myThread.getState());
// TIMED_WAITING
myThread.interrupt();
try {Thread.sleep(100);} catch (InterruptedException e) {}
System.out.println("MyThread State = " + myThread.getState());
}
}
BlockState.java
package thread;
class MyBlockTest {
// 공유 객체
MyClass mc = new MyClass();
// 3개의 쓰레드 필드 생성
Thread t1 = new Thread("thread1" ) {
public void run() {
mc.syncMethod();
};
};
Thread t2 = new Thread("thread3") {
public void run() {
mc.syncMethod();
};
};
Thread t3 = new Thread("thread3") {
public void run() {
mc.syncMethod();
};
};
void starAll() {
t1.start();
t2.start();
t3.start();
}
class MyClass {
synchronized void syncMethod() {
try {Thread.sleep(100);} catch (InterruptedException e) {}
System.out.println("====" + Thread.currentThread().getName() + "====");
System.out.println("thread1->" + t1.getState());
System.out.println("thread2->" + t2.getState());
System.out.println("thread3->" + t3.getState());
for(long i = 0; i < 1000000000L; i++) {} // 시간 지연
}
}
}
public class BlockState {
public static void main(String[] args) {
MyBlockTest mbt = new MyBlockTest();
mbt.starAll();
}
}
TwoGenericArguments.java
package generic;
class KeyValue<K, V> {
private K key;
private V value;
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
public class TwoGenericArguments {
public static void main(String[] args) {
KeyValue<String, Integer> kv1 = new KeyValue();
kv1.setKey("사과");
kv1.setValue(1000);
String key1 = kv1.getKey();
int value1 = kv1.getValue();
System.out.println("key: " + key1 + " value: " + value1);
KeyValue<Integer, String> kv2 = new KeyValue<>();
kv2.setKey(404);
kv2.setValue("Not Found(요청한 페이지를 찾을 수 없습니다.)");
int key2 = kv2.getKey();
String value2 = kv2.getValue();
System.out.println("key: " + key2 + " value: " + value2);
KeyValue<String, Void> kv3 = new KeyValue<>();
kv3.setKey("키 값만 사용");
String key3 = kv3.getKey();
System.out.println("key: " + key3);
}
}
GenericMethod.java
package generic;
class GenericMethods{
public <T> T method1(T t) {
return t;
}
public <T> boolean method2(T t1, T t2) {
return t1.equals(t2);
}
public <K,V> void method3(K k, V v) {
System.out.println(k + ":" + v);
}
}
public class GenericMethod {
public static void main(String[] args) {
GenericMethods gm = new GenericMethods();
String str1 = gm.<String>method1("안녕");
String str2 = gm.method1("안녕");
System.out.println(str1);
System.out.println(str2);
boolean bool1 = gm.<Double>method2(2.5, 2.5);
boolean bool2 = gm.method2(2.5, 2.5);
System.out.println(bool1);
System.out.println(bool2);
gm.<String, Integer>method3("국어", 80);
gm.method3("국어", 80);
}
}
BoundedTypeOfGenericClass.java
package generic;
class A{}
class B extends A{}
class C extends B{}
class D <T extends B> {
private T t;
public T get() {
return t;
}
public void set(T t) {
this.t = t;
}
}
public class BoundedTypeOfGenericClass {
public static void main(String[] args) {
D<B> d2 = new D<>();
D<C> d3 = new D<>();
D d4 = new D();
d2.set(new B());
d2.set(new C());
d3.set(new C());
d4.set(new B());
d4.set(new C());
}
}
BoundedTypeOfGenericMethod.java
package generic;
class AA {
public <T extends Number> void method1(T t) {
System.out.println(t.intValue());
}
}
interface MyInterface {
public abstract void print();
}
class BB {
public <T extends MyInterface> void method1(T t) {
t.print();
}
}
public class BoundedTypeOfGenericMethod {
public static void main(String[] args) {
AA a = new AA();
a.method1(5.8);
BB b = new BB();
b.method1(new MyInterface() {
@Override
public void print() {
System.out.println("print() 구현");
}
});
}
}