Apr 04

What is the output of the following Code?

public class Dog extends Thread{

public void run(){

for (int x = 0; x < 10; x++)

System.out.print(x + ” “);

}

public void start(){}

public static void main(String args[]) {

Dog d = new Dog();
d.start();

}

}

Antwort/ answer
Tagged with:
Apr 03

What is the output of the following code?

class Cat implements Runnable {

public void run () { System.out.println(Thread.currentThread().getName()); }

}

public class Dog {

public static void main(String args[]) {

Cat cat = new Cat();
Thread a = new Thread(cat);
Thread b = new Thread(cat);
a.setName(“Thread a”);
b.setName(“Thread b”);
a.setPriority(2);
b.setPriority(-1);
a.start();
b.start();

}

}

Antwort/ answer
Tagged with:
Apr 02

Not all methods are owned by class Thread. Therefore here a small overview which method belongs to which class:

Class Thread:
start();
yield();
sleep();
join();

Class Object:
wait();
notify();
notifyAll();

Interface Runnable:
run();

Tagged with:
Apr 01

Today I would like to go a little bit deeper into interaction-method, e.g. instead of method

wait();

it is also possible to wait only a specified time (in ms) with

wait(ms);

A special notification is not neccessary.

Furthermore instead of just notifiying the next waiting thread with

notify();

also all waiting threads could be woken up and started with

notifyAll();

Tagged with:
Mar 31

As already discussed there is a risk of Threads running into deadlocks, but as we say in German “gefahr gekannt, gefahr gebannt” which means something like “as soon as we know the risk we can do something against it; we can let them interact with each other;

class Shop implements Runnable {

Shop a = new Shop();// creating a common instance which can be accessed from everywhere

public void run () {

if (Thread.currentThread().getName().equals(“Mary”))

sales_flow(); // Mary is locking method sales_flow

else

check_stock(); // Steve is locking method check_stock

}

synchronized void sales_flow(){

if (Thread.currentThread().getName().equals(“Mary”))

check_stock(); // Mary is trying to access method check_stock which is already locked by Steve

}

synchronized void check_stock (){

a.notify(); // The next waiting Thread is informed that it can proceed although the method is not finished completely

if (Thread.currentThread().getName().equals(“Steve”))

sales_flow(); // Steve is trying to access method sales_flow which is already locked by Mary

}

public static void main (String args []){

Shop a = new Shop(); // This needs to be removed
Thread sale1 = new Thread(a);
sale1.setName(“Mary”);
sale1.start();

Thread sale2 = new Thread(a);
sale2.setName(“Steve”);
sale2.start();

synchronized (a){

try {

a.wait(); // The object is synchronized and waiting for other threads to finish (only in case there is another thread started before and running)

} catch (InterruptedException e ){ e.printStackTrace(); }

}

}

}

Tagged with:
Mar 30

A deadlock appears in case one two threads of the same instance are waiting for each others release of the lock:

class Shop implements Runnable {

public void run () {

if (Thread.currentThread().getName().equals(“Mary”))

sales_flow(); // Mary is locking method sales_flow

else

check_stock(); // Steve is locking method check_stock

}

synchronized void sales_flow(){

if (Thread.currentThread().getName().equals(“Mary”))

check_stock(); // Mary is trying to access method check_stock which is already locked by Steve

}

synchronized void check_stock (){

if (Thread.currentThread().getName().equals(“Steve”))

sales_flow(); // Steve is trying to access method sales_flow which is already locked by Mary

}

public static void main (String args []){

Shop a = new Shop();
Thread sale1 = new Thread(a);
sale1.setName(“Mary”);
sale1.start();

Thread sale2 = new Thread(a);
sale2.setName(“Steve”);
sale2.start();

}

}

Please be aware that in this case a deadlock CAN happen but there is no guarantee – in such a small programm it is very likely that Mary has done all her work before Steve is starting to lock something. If there would be some sleeps implemented or time-consuming operations are implemented it is very likely that the programm would go into deadlock.

Tagged with:
Mar 29

As already discussed, a class can not be synchronized on its own, but we are speaking about thread-safe classes in case the methods inside the class are carefully synchronized in order to protect the data.

A good example for (non-)Thread-safe classes are StringBuilder and StringBuffer.  While StringBuffer is Thread-safe StringBuilder is not. Since StringBuilder is not taking care of synchronizing, it is faster than StringBuffer who needs time to take care.

Tagged with:
Mar 28

Usually Threads are synchronized in order to prevent methods from interrupting each other and to mess up data. Please keep in this context in mind that static and non-static methods are never blocking each other as discussed the post before.

In order to change a static variable you should use a static method to change it and to change a non-static variable a non-static method should be used and synchronized. Otherwise things can mess up quite quickly.

Tagged with:
Mar 27

static synchronized methods are never blocking non static synchronized methods since once the lock is done per class (static) and once by instance (non-static)

Tagged with:
Mar 26

while non-static synchronized methods can only be locked per instance, static synchronized methods are locked by class, not by instance since such methods are always using the same class instance. Therefore one locked static synchronized method is locking all other static synchronized methods.

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs