Java and C# in Depth
- Prof. Dr. Bertrand Meyer
Chair of Software Engineering
Exercise Session 8 Nadia Polikarpova Quiz 1: What is printed? - - PowerPoint PPT Presentation
Chair of Software Engineering Java and C# in Depth Prof. Dr. Bertrand Meyer Exercise Session 8 Nadia Polikarpova Quiz 1: What is printed? (Java) class MyTask implements Runnable { Everything is ok! public void run() { Exception
Chair of Software Engineering
2
Java and C# in depth
class MyTask implements Runnable { public void run() {
} } public static void main(String[] args) { try {
} catch (RuntimeException e) {
} }
«Everything is ok! Exception in thread...: Help!» Exceptions from other threads are not propagated to main
3
Java and C# in depth
static void MyTask() { throw new Exception("Help!"); } delegate void MyTaskInvoker(); public static void Main() { try { MyTaskInvoker method = MyTask; IAsyncResult res = method.BeginInvoke(null, null); method.EndInvoke(res);
// new Thread(MyTask).Start(); } catch (Exception) { Console.WriteLine("Something went wrong"); }
4
Java and C# in depth
static void MyTask() { try {
} catch { ... } finally {
} } public static void Main() { Thread t = new Thread(MyTask); t.IsBackground = true; t.Start(); ... t.Interrupt(); }
finally block may not be executed: the main thread may exit before that and the application does not wait for background threads to finish
5
Java and C# in depth
Shared variable
Dont expect that the first interrupt we get is the one we need: use while instead of if To call wait the enclosing method must be synchronized (otherwise IllegalMonitorStateException is thrown at runtime)
6
Java and C# in depth
7
Java and C# in depth
class MyTask implements Runnable { public void run() {
} } public static void main(String[] args) { try {
} catch (InterruptedException e) {} }
run does not handle interrupts this code is never executed
8
Java and C# in depth
public synchronized void run() { while (true) try {
} catch (InterruptedException e) {
} }
public void run() { while (true) {
} }
9
Java and C# in depth
This code is executed. Unlike Interrupt, Abort stops the thread even if its currently running
10
Java and C# in depth
static void Run() { while (true) {
} } public static void Main() { Thread t = new Thread(Run); t.Start(); Thread.Sleep(500); t.Abort(); t.Join(); Console.WriteLine("t aborted"); }
Thread t is still aborted! ThreadAbortException is automatically rethrown at the end of the catch block if Thread.ResetAbort is not called
11
Java and C# in depth
c++ is not atomic => the result might be 1
12
Java and C# in depth
All attributes must be accessible only through synchronized methods
private
13
Java and C# in depth
class Counter { ... // Everything as before public static synchronized void increment_some(Counter count) {
} Counter(int c) {
} }
No: static methods use different
OK: constructors need not (and cannot) be synchronized, they are executed once per object
14
Java and C# in depth
public class BadThreads { static String message; private static class CorrectorThread extends Thread {
} public static void main(String args[]) throws InterruptedException {
} }
How to ensure that “Mares do eat oats” is always printed?
15
Java and C# in depth
public static void main(String args[]) throws InterruptedException { CorrectorThread ct = new CorrectorThread(); ct.start();
ct.join(); System.out.println(message); }
Guarantees that the 2 threads see the same value of message, but does not guarantee that message = "Mares do eat oats."; is executed before System.out.println(message); (it is not guaranteed that sleep (2000) takes longer than sleep (1000))
16
Java and C# in depth
Here we want to see the change to dt made by the main thread
Compilation error: Objects of non-primitive value types cannot be cached by the processor => need not (and cannot) be volatile
17
Java and C# in depth