package demo.sync;
public class ATM implements Runnable {
private long balance = 1000000;
@Override
public void run() {
for (int i=0; i<25; i++) {
출금();
}
}
public synchronized void 출금() {
System.out.println("현재 잔액 : " + balance);
System.out.println("출금 시작");
balance -= 10000;
System.out.println("출금 종료");
System.out.println("출금 후 잔액 : " + balance);
}
}
package demo.sync;
public class ATMApp {
public static void main(String[] args) {
ATM atm = new ATM();
Thread t1 = new Thread(atm);
Thread t2 = new Thread(atm);
Thread t3 = new Thread(atm);
Thread t4 = new Thread(atm);
t1.start();
t2.start();
t3.start();
t4.start();
}
}
package demo.sync;
import java.util.ArrayList;
import java.util.Vector;
public class ListRunnable implements Runnable {
@Override
public void run() {
ArrayList<String> names = new ArrayList<String>();
for(int i=0; i<100; i++) {
names.add("홍길동" + i);
}
System.out.println(names.size());
}
}
package demo.sync;
public class ListRunnableApp {
public static void main(String[] args) {
ListRunnable work = new ListRunnable();
Thread t1 = new Thread(work);
Thread t2 = new Thread(work);
Thread t3 = new Thread(work);
t1.start();
t2.start();
t3.start();
}
}
'자바 > network' 카테고리의 다른 글
thread (0) | 2019.06.10 |
---|---|
store (0) | 2019.06.10 |
simpleClient (0) | 2019.06.10 |
mallClient (0) | 2019.06.10 |
file (0) | 2019.06.10 |