Comparing C# and Java
In principle, the treading models of C# and Java are similar.
Only some details are different.
C# |
Java |
Any parameterless void method can be started as a thread.
The predeclared class Thread does not need to be extended. |
Threads must be implemented as subclasses of the
predeclared class Thread. The tread actions must be implemented
in the Run method. |
If a thread needs to have a state, this state information
must be stored in the fields of the class to which the thread method belongs. |
If a thread needs to have a state, this state information
must be stored in the fields of the subclass of Thread. |
The lock statement guarantees that only one of several
threads is in a critical region at any time. |
The synchronized statement of Java corresponds
to the lock statement of C#. In Java, a whole method can be
declared to be synchronized which is equivalent with a synchronized
statement around the body of the method. |
The lock statement can set a lock on an arbitrary object. |
The synchronized statement sets a lock on the object this. |
Wait and Pulse are methods of the Monitor class.
They can be applied to arbitrary objects on which a lock was set. |
wait and notify are methods of the Object class.
They are implicitly applied to this, which is the object on
which the lock was set. |
C# distinguishes between foreground und background threads. |
Java distinguishes between user and demon threads.
The meaning is roughly the same. |
|