thread
-
Java - Network - File I/OJava 2018. 2. 7. 10:46
// Network를 통한 File I/O는// Socket을 통해서 주고 받는다 import java.io.BufferedInputStream;import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.net.Socket; public class SenderMain { public static void main(String[] args) throws Exception { // 1. 경로 지정 String path = "D:/io/bang.gif"; File file = new File(path); // 2. 스트림 준비 FileInputStream fis = new FileInputStream..
-
Java - UDPJava 2018. 2. 6. 14:53
// UDP는 비연결 지향 프로토콜// 파일 전송 같은 경우 깨져서 들어옴// 이어 오기 X, 속도가 중요하다 import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress; public class Sender { public static void main(String[] args) { // UDP를 사용하려면 DataGram 객체를 사용 // 1. DataGramSocket 생성 // 2. 보낼 내용 준비 // 3. 보낼 내용을 DataGramPacket에 담는다. // 4. 패킷을 전송 // 5. 자원 정리(close) try (DatagramSocket socket = new Datagram..
-
Java - Thread Pool, Thread Pool BlockingJava 2018. 1. 31. 14:51
// Thread pool은 대여소 같은 역할을 수행 한다.// 대여할 Thread가 없다면 대기해야 한다.// Thread pool은 ExecutorService 객체를 통해 생성 된다.// 생성시에는 초기/코어/최대 스레드 수를 명시해 준다. // 코어 스레드 수 - 최소한으로 유지되어야 하는 수 // 동시 수행 - 실시간(Real Time), 병렬 처리// 프로그램상 스레드는 실제 스레드에서 남는 부분에서 수행한다.// 그래서 여러 스레드를 만들 수 있다. // Thread Pool의 작업은 Runnable과 Callable로 생성 한다.// 두 방법의 차이는 return 값의 유무 이다.// Runnable은 return값이 없고 Callable은 있다. // 생성된 ..
-
Java - Deamon Thread, Thread GroupJava 2018. 1. 31. 13:14
// deamon thread는 main thread작업을 돕는 보조적인 역할을 수행 한다.// Work Thread와 마찬가지로 main thread에 의해서 생성 된다// 다만 life cycle의 차이가 있다. // deamon thread는 main thread가 끝날때 같이 끝난다.// Work thread는 작업이 다 끝나야 thread가 끝난다. public class DeamonThread extends Thread { @Override public void run() { while(true) { try { Thread.sleep(500); System.out.println("Thread Working..."); } catch (InterruptedExcept..
-
Java - Stop, InterruptJava 2018. 1. 31. 12:07
public class StopFlag extends Thread { boolean stop = false; @Override public void run() { while(!stop) { System.out.println("Thread Running..."); } System.out.println("종료"); } } public class Main { public static void main(String[] args) throws InterruptedException { StopFlag stop = new StopFlag(); stop.start(); Thread.sleep(1000); stop.stop = true; } } // InterruptedException을 발동 ..
-
Java - Wait, NotifyJava 2018. 1. 31. 10:38
public class CommonData { private String data; //synchronized 는 차후 다수의 스레드가 접근할 여지가 있는지 확인 해 볼것 public synchronized void getData() { try { //1. 데이터가 있는지 확인 if(data != null) { //2. 있으면 가져오고 없으면 가져오지 않는다. System.out.println("가져 온 값 : "+data); data = null;//가져온 후 값을 비워 준다. } //3. 상대를 깨우고 나는 쉰다. notify(); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } public synchronized void se..
-
Java - ThreadJava 2018. 1. 30. 14:57
// Thread는 어떤 program을 실행 하게 해주는 원동력이다// 대표적으로 main(String[] args)로 실행 한다. // process는 1개 이상의 thread로 구성되어 있다.// process는 thread로 구성 된다.// 메모리 공유 부분에서 process는 메모리 공유가 안되지만,// Thread는 메모리 공유가 된다. // main method는 main thread를 생성 한다.// 처리가 버거우면 work thread를 생성할 수 있다.// 이것을 multi thread라 한다. // Runnable Interface, Thread Class 상속으로 생성할 수 있다. public class Main { public static void main(..