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? -
IT Certification Category (English)640x480

Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com


Top Online Courses From ProgrammingKnowledge

Python Course http://bit.ly/2vsuMaS
Java Coursehttp://bit.ly/2GEfQMf
Bash Coursehttp://bit.ly/2DBVF0C
Linux Coursehttp://bit.ly/2IXuil0
C Course http://bit.ly/2GQCiD1
C++ Coursehttp://bit.ly/2V4oEVJ
PHP Coursehttp://bit.ly/2XP71WH
Android Coursehttp://bit.ly/2UHih5H
C# Coursehttp://bit.ly/2Vr7HEl
JavaFx Coursehttp://bit.ly/2XMvZWA
NodeJs Coursehttp://bit.ly/2GPg7gA
Jenkins Course http://bit.ly/2Wd4l4W
Scala Coursehttp://bit.ly/2PysyA4
Bootstrap Coursehttp://bit.ly/2DFQ2yC
MongoDB Coursehttp://bit.ly/2LaCJfP
QT C++ GUI Coursehttp://bit.ly/2vwqHSZ