본문 바로가기

Study/Java

[java] 스레드, 소켓 실습

멀티스레드

Account.java

public class Account {
 String accountNo;
 String ownerName;
 int balance;
 
 Account (String accountNo, String ownerName, int balance) {
  this.accountNo = accountNo;
  this.ownerName = ownerName;
  this.balance = balance;
 }
 void desposit(int amount) {
  balance += amount;
 }
 int withdraw(int amount) {
  if (balance < amount)
   return 0;
  balance -= amount;
  return amount;
 }
}

공유 영역 클래스

SharedArea.java

class SharedArea {
 Account account1;
 Account account2;
}

계좌이체를 수행하는 스레드

TransferThread.java

class TransferThread extends Thread {
 SharedArea sharedArea;
 TransferThread(SharedArea area) {
  sharedArea = area;
 }
 public void run() {
  for (int cnt=0;cnt<12;cnt++) {

//스레드의 동기화 
   synchronized (sharedArea) {

//동기화 블록
    sharedArea.account1.withdraw(1000000);
    System.out.print("이몽룡 계좌 : 100만원 인출, ");
    sharedArea.account2.desposit(1000000);
    System.out.println("성춘향 계좌 : 100만원 입금");
   }
  }
 }
}

계좌 잔액 합계를 출력하는 스레드

PrintThread.java

class PrintThread extends Thread {
 SharedArea sharedArea;
 PrintThread(SharedArea area) {
  sharedArea = area;
 }
 public void run() {
  for (int cnt=0;cnt<3;cnt++) {

//스레드의 동기화
   synchronized (sharedArea) {

//동기화블록    
    int sum = sharedArea.account1.balance +
    sharedArea.account2.balance;
    System.out.println("계좌 잔액 합계 : " +sum);
   }
   try {

//스레드가 경과되는 시간
    Thread.sleep(1);
   } catch(Exception e) {
    System.out.println(e.getMessage());
   }
  }
 }
}

MultithreadExample.java

public class MultithreadExample {
 public static void main(String[] args) {
  SharedArea area = new SharedArea();
  area.account1 = new Account("111-111-1111", "이몽룡", 20000000);
  area.account2 = new Account("222-222-2222", "성춘향", 10000000);
  TransferThread thread1 = new TransferThread(area);
  PrintThread thread2 = new PrintThread(area);

//스레드의 시작
  thread1.start();
  thread2.start();
 }
}

 

소켓

ServerExample.java

public class ServerExample {

 public static void main(String[] args) {
  ServerSocket serverSocket = null;
  Socket socket = null;
  try {
   serverSocket = new ServerSocket(9000);
   socket = serverSocket.accept();
   InputStream in = socket.getInputStream();
   OutputStream out = socket.getOutputStream();
   byte arr[] = new byte[100];
   in.read(arr);
   System.out.println(new String(arr));
   String str = "Hello, Client";
   out.write(str.getBytes());
  } catch (Exception e) {
   System.out.println(e.getMessage());
  }
  finally {
   try {
    socket.close();
   } catch(Exception e) {}
   try {
    serverSocket.close();
    } catch(Exception e) {}
  }
 }
}

ClientExample.java

public class ClientExample {

 public static void main(String[] args) {
  Socket socket = null;
  try {
   socket = new Socket("127.0.0.1", 9000);
   InputStream in = socket.getInputStream();
   OutputStream out = socket.getOutputStream();
   String str = "Hello, Server";
   out.write(str.getBytes());
   byte arr[] = new byte[100];
   in.read(arr);
   System.out.println(new String(arr));
  } catch(Exception e) {
   System.out.println(e.getMessage());
  }
  finally {
   try {
    socket.close();
   } catch(Exception e) {}
  }
 }
}

'Study > Java' 카테고리의 다른 글

[java] 지도의 위도,경도로 원형 좌표 거리 계산 및 좌표변환  (0) 2019.05.21
[java] JAVA 꼭 알아야할 것  (0) 2019.05.15
[java] 스레드, 소켓  (0) 2019.05.15
[java] Input, Output 실습  (0) 2019.05.15
[java] Input, Output  (0) 2019.05.15