본문 바로가기

Development/Programming

[Java Design Patten] Worker Thread 패턴 Worker Thread 패턴에서는 Worker thread가 일을 하니씩 가지러 가고, 또 처리합니다. 일이 없으면 Worker thread는 새로운 일이 올때까지 대기합니다. Client 역할업무를 작성하여 Channel역할에게 일을 전 달 public class ClientThread extends Thread {private final Channel channel;private static final Random random = new Random();public ClientThread (final String name, final Channel channel) {super(name);this.channel = channel;} public void run () {try {for (int i=0;.. 더보기
[Java Design Patten] Thread per Message 패턴 이번 패턴을 간단하게 요약해서 이야기하면"어떠한 명령이나 요구마다 새로 한개의 쓰레드가 할당되고 그 쓰레드가 처리를 실행한다." 역할은 Client, Host, Helper로 나눌 수 있다. Client는 Request를 요구하는 역할이다. 여기선 Main이 담당 public class Main {public static void main (final String[] args) {final Host host = new Host();host.request(10,'A');host.request(20,'B');host.request(30,'C');} } Host는 Client로부터 Request를 받으면 새로운 Thread를 생성하는 역할 public class Host {private final Helper.. 더보기
[Java Design Patten] Read-Write Lock 패턴 여러개의 쓰레드가 동시에 읽는 것은 문제가 없습니다. 하지만 읽는 중간에 쓰게 되면 문제가 발생하기에 이런 경우에 사용하면 효과적이다. 읽기 쓰기 Lock을 담당 public final class ReadWriterLock {private int readingReaders = 0;private int waitingWriters = 0;private int writingWriters = 0;private boolean preferWriter = true;public synchronized void readLock () throws InterruptedException {while (writingWriters > 0 || (preferWriter && waitingWriters > 0)) {wait();}r.. 더보기
[Java Design Patten] Producer - Consumer 패턴 이 패턴에서는 Producer와 Consumer 그리고 Channel의 역할이 필요하다. Channel의 역할이 필요한 이유 Producer역할이 Consumer 역할의 처리 상황에 좌우되지 않게 하기 위함 Producer 역할 public class MakerThread extends Thread {private final Random random;private final Table table;private static int id = 0;public MakerThread (final String name, final Table table, final long seed) {super(name);this.table = table;this.random = new Random(seed);}public voi.. 더보기
[Java Design Patten] Balking 패턴 지금 이 처리를 실행하면 곤란하다거나 혹은 당장 실행할 필요가 없거나 혹은 다른 곳에서 이미 처리를 하고 있는 경우 실행을 중단하고 돌아가는 패턴이다. 가드 되어 있는 객체 역할 이며 guardedMethod와 stateChangingMethod 를 가지고 있는 패턴이다. 여기서는 save와 chage 함수를 보면 된다.public class Data {private final String filename;private String content;private boolean changed;public Data (final String filename, final String content) {this.filename = filename;this.content = content;this.changed = .. 더보기
[Java Design Patten] Guarded Suspension 이 처리를 실행하면 안 될 때 처리하기 직전에 쓰레드를 기다리게 하는 패턴이다. 아래의 시퀀스 다이어그램을 참고~! public class Request {private final String name;public Request (final String name) {this.name = name;}public String getName () {return this.name;} public String toString() {return "Request [name=" + name + "]";} } public class RequestQueue { private final Queue queue = new LinkedList();public synchronized Request getRequest () {whi.. 더보기
[Java Design Patten] Immutable 상태의 변화가 없는 경우에는 synchronized를 할 필요가 없다. 여기서는 불변의 객체의 역할로 Person이 담당을 하였다. public final class Person {private final String name;private final String address;public Person (final String name, final String address) {this.name = name;this.address = address;} public String getName() {return name;} public String getAddress() {return address;} @Overridepublic String toString() {return "Person [name=" +.. 더보기
[Java Design Patten] Single Threaded Execution Gate 클래스는 SharedResource 역할을 담당하고 있다. 그리고 자세히 보면 pass 에 Synchronized 키워드를 사용한 것을 주목하자 사용한 이유는 동시에 복수의 Thread가 접근하면 문제가 발생하기 때문이다. public class Gate {private int counter = 0;private String name = "Nobody";private String address = "NoWhere";public synchronized void pass (final String name, final String address) {this.counter++;this.name = name;this.address = address;check();}public String toString.. 더보기
[Java] wait, notify, notifyAll Thread가 Wait를 하면 다른 Thread에서 notify 혹은 notifiyAll을 호출해야 다음 작업을 진행한다. notify 와 notifiyAll의 차이는 하나만 다시 실행하게 할지 아니면 기다리고 있는 모든 Thread를 실행할지 의 차이이며notifiyAll을 사용하는 쪽이 코드가 견고하다고 한다. 그리고 wait메소드를 실행하기 위해서는 반드시 락을 가지고 있어야 한다. 더보기
[Java] synchronized 사용법 정리 1. synchronized void method () {.....} 2. void emthod () {synchronized (this) {....}} 3. class Something {static synchronized void method () {...}} 4. class Something {static void method () {synchronized (something.class) {...}}} 더보기