15
Multithreading QUIZ QUIZ

Multi Threading Quiz

Embed Size (px)

Citation preview

Page 1: Multi Threading Quiz

Multithreading

QUIZQUIZ

Page 2: Multi Threading Quiz

QUIZQUIZHow many threads?

public static void main(String[] args) {

y

1. NoneRunnable r1 = new Producer();Runnable r2 = new Producer();Thread t1 = new Thread(r1);

2. One3. Two Thread t1 = new Thread(r1);

Thread t2 = new Thread(r2);

t1.start();

3. Two4. Three5 I don’t know

t2.start();

}

5. I don t know

}

Page 3: Multi Threading Quiz

QUIZQUIZHow many threads?

public static void main(String[] args) {

y

1. NoneRunnable r1 = new Producer();Runnable r2 = new Producer();Thread t1 = new Thread(r1);

2. One3. Two Thread t1 = new Thread(r1);

Thread t2 = new Thread(r2);

t1.start();

3. Two4. Three5 I don’t know

t2.start();

}

5. I don t know

}

Page 4: Multi Threading Quiz

QUIZQUIZ

Which thread(s)become blocked?

Thread T1 based on run methodpublic void run {

try { Thread.sleep(100000); } 1. Noney { p( ); }catch (InterruptedException e) {}

}

1. None2. T13. T2

Thread T2 based on run method

3. T24. Both5 I don’t know

public void run {factor(785678564567385635789)

}

5. I don t know

}factor method from ”noter” ch. 1

Page 5: Multi Threading Quiz

QUIZQUIZ

Which thread(s)become blocked?

Thread T1 based on run methodpublic void run {

try { Thread.sleep(100000); } 1. Noney { p( ); }catch (InterruptedException e) {}

}

1. None2. T13. T2

Thread T2 based on run method

3. T24. Both5 I don’t know

public void run {factor(785678564567385635789)

}

5. I don t know

}factor method from ”noter” ch. 1

Page 6: Multi Threading Quiz

public class Test1 implements Runnable {public void run() {

QUIZ

p () {try {

for (int i=1; i<=10; i++) {S t t i tl (i)QUIZ System.out.println(i);Thread.sleep(1000);

}} catch (InterruptedException e) {

} } }

Th t h t t t i t ?The catch statement is empty?1. Compiler error2. Legal code, but better style to omit try-catch3. Legal code, try-catch is necessary, but empty catch is

bad style4. Legal code, try-catch is necessary, empty catch is good

t lstyle5. I don’t know

Page 7: Multi Threading Quiz

public class Test1 implements Runnable {public void run() {

QUIZ

p () {try {

for (int i=1; i<=10; i++) {S t t i tl (i)QUIZ System.out.println(i);Thread.sleep(1000);

}} catch (InterruptedException e) {

} } }

Th t h t t t i t ?Good style to swallow Exception

The catch statement is empty?1. Compiler error

only in this context.InterruptedException indicates that

the thread should terminate

2. Legal code, but better style to omit try-catch3. Legal code, try-catch is necessary, but empty catch is

bad style4. Legal code, try-catch is necessary, empty catch is good

t lstyle5. I don’t know

Page 8: Multi Threading Quiz

QUIZ Could race condition occur whenrunning these thread in parallel?QUIZ

Producer Thread:BoundedQueue<String> queue

running these thread in parallel?1. Yes2. No3. I don’t knowBoundedQueue<String> queue =

new BoundedQueue<String>(10);for (int i = 1; i <= 100; i++) {

queue.add(i + ": " + greeting);Thread.sleep((int)(Math.random() * DELAY));

}}

Consumer Thread:d d iBoundedQueue<String> queue = 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));

}

Page 9: Multi Threading Quiz

QUIZ Could race condition occur whenrunning these thread in parallel?QUIZ

Producer Thread:BoundedQueue<String> queue

running these thread in parallel?1. Yes2. No3. I don’t knowBoundedQueue<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}

Consumer Thread:d d i

No race condition, since the threads do not use the same

queue object. (This may be a logical error)BoundedQueue<String> queue =

new BoundedQueue<String>(10);for (int i = 1; i <= 100; i++) {

logical error)

Object greeting = queue.remove();System.out.println(greeting); Thread.sleep((int)(Math.random() * DELAY));

}

Page 10: Multi Threading Quiz

QUIZ Does it work?QUIZ Does it work?

1: No, race condition may still h k i i id occur

2: Yes race condition cannot

isfull() check is now insidelock protected add()

public void add(E newValue) throws InterruptedException {

2: Yes, race condition cannot occur, and this code works fine

aLock.lock();try {

while( isFull() ) {3: ??? – race condition cannot

occur – but a new problem( () ) {Thread.sleep(1000);

}

occur but a new problem arises…

...} finally {aLock.unlock();}

}

4: I don’t know

Page 11: Multi Threading Quiz

QUIZ Does it work?QUIZ Does it work?

1: No, race condition may still h k i i id occur

2: Yes race condition cannot

isfull() check is now insidelock protected add()

public void add(E newValue) throws InterruptedException {

2: Yes, race condition cannot occur, and this code works fine

aLock.lock();try {

while( isFull() ) {3: ??? – race condition cannot

occur – but a new problem( () ) {Thread.sleep(1000);

}

occur but a new problem arises…

...} finally {aLock.unlock();}

}

4: I don’t know

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

Page 12: Multi Threading Quiz

QUIZQUIZWhat is not reason for blocking a thread?g

1. Sleeping 2. Waiting for I/O2. Waiting for I/O3. Time slice is up 4. Waiting to acquire lock4. Waiting to acquire lock5. Waiting for a condition6 I don’t know6. I don t know

Page 13: Multi Threading Quiz

QUIZQUIZWhat is not reason for blocking a thread?g

1. Sleeping 2. Waiting for I/O2. Waiting for I/O3. Time slice is up 4. Waiting to acquire lock4. Waiting to acquire lock5. Waiting for a condition6 I don’t know6. I don t know

Page 14: Multi Threading Quiz

QUIZQUIZWhen is InterruptedException thrown?p p

1. when Thread.sleep(..) returns2. when <condition>.await() returns2. when condition .await() returns 3. when other thread calls this threads interrupt

method while this thread is blocked4. I don’t know

Page 15: Multi Threading Quiz

QUIZQUIZWhen is InterruptedException thrown?p p

1. when Thread.sleep(..) returns2. when <condition>.await() returns2. when condition .await() returns 3. when other thread calls this threads interrupt

method while this thread is blocked4. I don’t know