Java Threading

what is thread safe in Java Threading?How threading works in java?One example will be appreciated.
[133 byte] By [tvs_2001] at [2007-9-26 5:02:13]
# 1

> what is thread safe in Java Threading?

Any piece of code that can be executed by multiple threads without having any shairing violations or deadlocks etc. is known to be thread safe

> How threading works in java?

Long debate, look into the Thread class and related documentations

> One example will be appreciated.

Well, only because its a weekend.... ;-) but I believe that you can find alot better examples and documentation if you have a look into the Tutorials on the site

public class Tester

{

public static void main(String[] args)

{

MyThread mt1 = new MyThread();

MyThread mt2= new MyThread();

MyThread mt3= new MyThread();

System.out.println("mt1");

mt1.start();

System.out.println("mt2");

mt2.start();

System.out.println("mt3");

mt3.start();

}

public static void method()

{

System.out.println(((MyThread)Thread.currentThread()).myNumber);

}

}

class MyThread extends Thread

{

static int count = 0;

public int myNumber = 0;

public MyThread()

{

myNumber = ++count;

}

public void run()

{

System.out.println("Thread is running");

Tester.method();

}

}

Hope you find this helpful, and have a good weekend.... :-)

Omer

Oaq at 2007-6-29 18:59:22 > top of Java-index,Archived Forums,Java Programming...
# 2

> what is thread safe in Java Threading?

When two threads can run safely without causing deadlocks, crashes, etc...

> How threading works in java?

Same as anything else: Two threads run at the same time (logically, anyway).

> One example will be appreciated.

Say you have a word processor that spell-checks and allows the user to enter text at the same time. When the word processor detects (for example) a space was typed in, it starts a thread, gives it the word the user just typed in, and continues to process the keystrokes. At the same time, the new thread goes to the dictionary of words and tries to find the word just typed in and if it is not found registers it as a misspelled word.

gmagana at 2007-6-29 18:59:22 > top of Java-index,Archived Forums,Java Programming...