반응형
쓰레드의 동기화
Backend/java2018. 8. 16. 04:16쓰레드의 동기화

쓰레드의 동기화 – synchronized 1. 한 번에 하나의 쓰레드만 객체에 접근할 수 있도록 객체에 락(lock)을 걸어서 데이터의 일관성을 유지하는 것 쓰레드 동기화 예제 package Practice;public class synchronizedExample implements Runnable{Account acc = new Account();public void run() {while(acc.balance>0) {int money = (int)(Math.random()*3 +1)*100; // 100,200,300 acc.withdraw(money);System.out.println("balance : " + acc.balance);}}}class Account {int balance = 10..

자격증/OCJP_ExamB2018. 7. 9. 20:45QUESTION 16

QUESTION 16 Given: 1. public class TestFive { 2. private int x; 3. public void foo() { 4. int current = x; 5. x = current + 1; 6. } 7. public void go() { 8. for(int i = 0; i < 5; i++) { 9. new Thread() { 10. public void run() { 11. foo(); 12. System.out.print(x + ", "); 13. } }.start(); 14. } } 15. } Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ? (Choose two.) A...

자격증/OCJP_ExamB2018. 7. 9. 20:34QUESTION 15

QUESTION 15 Which three will compile and run without exception? (Choose three.) A. private synchronized Object o; B. void go() { synchronized() { /* code here */ } C. public synchronized void go() { /* code here */ } D. private synchronized(this) void go() { /* code here */ } E. void go() { synchronized(Object.class) { /* code here */ } F. void go() { Object o = new Object(); synchronized(o) { /..

반응형
image