Sie sind auf Seite 1von 15

Multithreading

QUIZ

QUIZ
How many threads? y
public static void main(String[] args) {

1. 2. 3. 4. 5. 5

None One Two Three I dont know don t


}

Runnable r1 Runnable r2 Thread t1 = Thread t2 = t1.start(); t2.start();

= new Producer(); = new Producer(); new Thread(r1); new Thread(r2);

QUIZ
How many threads? y
public static void main(String[] args) {

1. 2. 3. 4. 5. 5

None One Two Three I dont know don t


}

Runnable r1 Runnable r2 Thread t1 = Thread t2 = t1.start(); t2.start();

= new Producer(); = new Producer(); new Thread(r1); new Thread(r2);

QUIZ
Which thread(s) become blocked?
1. 2. 3. 4. 5. 5 None T1 T2 Both I dont know don t

Thread T1 based on run method public void run { try { Thread.sleep(100000); } y p( ); catch (InterruptedException e) {} }

Thread T2 based on run method public void run { factor(785678564567385635789) } factor method from noter ch. 1

QUIZ
Which thread(s) become blocked?
1. 2. 3. 4. 5. 5 None T1 T2 Both I dont know don t

Thread T1 based on run method public void run { try { Thread.sleep(100000); } y p( ); catch (InterruptedException e) {} }

Thread T2 based on run method public void run { factor(785678564567385635789) } factor method from noter ch. 1

QUIZ

public class Test1 implements Runnable { p public void run() { () try { for (int i=1; i<=10; i++) { System.out.println(i); S t t i tl (i) Thread.sleep(1000); } } catch (InterruptedException e) { } } }

The t h t t Th catch statement is empty? ti t ?


1. Compiler error 2. Legal code, but better style to omit try-catch 3. Legal code, try-catch is necessary, but empty catch is bad style 4. Legal code, try-catch is necessary, empty catch is good style t l 5. I dont know

public class Test1 implements Runnable { p public void run() { () try { for (int i=1; i<=10; i++) { System.out.println(i); S t t i tl (i) Thread.sleep(1000); } } catch (InterruptedException e) { } } } Good style to swallow Exception only in this context. The t h t t Th catch statement is empty? ti t ? InterruptedException indicates that 1. Compiler error the thread should terminate

QUIZ

2. Legal code, but better style to omit try-catch 3. Legal code, try-catch is necessary, but empty catch is bad style 4. Legal code, try-catch is necessary, empty catch is good style t l 5. I dont know

QUIZ
Producer Thread:

BoundedQueue<String> queue = new BoundedQueue<String>(10); for (int i = 1; i <= 100; i++) { queue.add(i + ": " + greeting); Thread.sleep((int)(Math.random() * DELAY)); }

Could race condition occur when running these thread in parallel? 1. Yes 2. No 3. I dont know

Consumer Thread:
BoundedQueue<String> queue = d d i new BoundedQueue<String>(10); for (int i = 1; i <= 100; i++) { Object greeting = queue.remove(); System.out.println(greeting); Thread.sleep((int)(Math.random() * DELAY)); }

QUIZ
Producer Thread:

BoundedQueue<String> queue = new BoundedQueue<String>(10); for (int i = 1; i <= 100; i++) { queue.add(i + ": " + greeting); Thread.sleep((int)(Math.random() * DELAY)); } No race condition since the condition, threads do not use the same queue object. (This may be a Consumer Thread: logical error) BoundedQueue<String> queue = d d i new BoundedQueue<String>(10); for (int i = 1; i <= 100; i++) { Object greeting = queue.remove(); System.out.println(greeting); Thread.sleep((int)(Math.random() * DELAY)); }

Could race condition occur when running these thread in parallel? 1. Yes 2. No 3. I dont know

QUIZ
isfull() check i now i id h k is inside lock protected add()

Does it work? 1: No, race condition may still occur 2: Yes, race condition cannot Yes occur, and this code works fine 3: ??? race condition cannot occur but a new problem arises 4: I dont know

public void add(E newValue) throws InterruptedException { aLock.lock(); try { while( isFull() ) { ( () Thread.sleep(1000); } ... } finally {aLock.unlock();} }

QUIZ
isfull() check i now i id h k is inside lock protected add()

Does it work? 1: No, race condition may still occur 2: Yes, race condition cannot Yes occur, and this code works fine 3: ??? race condition cannot occur but a new problem arises 4: I dont know

public void add(E newValue) throws InterruptedException { aLock.lock(); try { while( isFull() ) { ( () Thread.sleep(1000); } ... } finally {aLock.unlock();} }

Deadlock: Deadlock Since the q e e remains locked no cons mer thread queue locked, consumer can execute remove() and make isFull() false

QUIZ
What is not reason for blocking a thread? g 1. Sleeping 2. Waiting for I/O 3. Time slice is up 4. Waiting to acquire lock 5. Waiting for a condition 6. dont 6 I don t know

QUIZ
What is not reason for blocking a thread? g 1. Sleeping 2. Waiting for I/O 3. Time slice is up 4. Waiting to acquire lock 5. Waiting for a condition 6. dont 6 I don t know

QUIZ
When is InterruptedException thrown? p p 1. when Thread.sleep(..) returns 2. when <condition>.await() returns condition .await() 3. when other thread calls this threads interrupt method while this thread is blocked 4. I dont know

QUIZ
When is InterruptedException thrown? p p 1. when Thread.sleep(..) returns 2. when <condition>.await() returns condition .await() 3. when other thread calls this threads interrupt method while this thread is blocked 4. I dont know

Das könnte Ihnen auch gefallen