Monday, June 1, 2015

Java Threads Tutorial 2 - How to Create Threads in Java by Extending Thr...




void start()
Creates a new thread and makes it runnable
This method can be called only once.

void run()
The new thread begins its life inside this method.

void stop() (deprecated)
The thread is being terminated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package lesson1;
/*
* Threads in Java
* Option 1 – extending class Thread
*/
class MyClass extends Thread {
@Override
public void run() {
for(int i=0 ; i<10 ; i++) {
System.out.println(Thread.currentThread().getId() + " Value : " + i );
}

try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// the super doesn't anything,

// but just for the courtesy and good practice

super.run();
}

}

public class Demo {

public static void main(String[] args) {
MyClass myClass = new MyClass();
myClass.start(); // don't call run! // (if you want a separate thread)
MyClass myClass1 = new MyClass();
myClass1.start();

}

}











------------------------------------------------
Searches related to creating first thread java
creating a thread in java example
creating thread in java using runnable interface
how to create a thread in java program
create thread in java web application
thread in java tutorial
thread in java tutorial pdf
thread in java tutorial
how to create a thread in android
How do I create a thread in Java? -

 

© 2013 Klick Dev. All rights resevered.

Back To Top