1public class ClassName extends Thread{
2 public void run(){
3 super.run();
4 //Your Code
5 }
6}
7
8public class Main{
9 public static void main(String[] args){
10 ClassName thread = new ClassName();
11 thread.start();
12 }
13}
1// Copy and test
2// They run simultaneously
3
4public static void main(String[] args) {
5 // How to create a thread
6 Thread thread = new Thread(new Runnable() {
7 @Override
8 // Loop running in thread
9 public void run() {
10 for (int i = 0; i < 20; i++) {
11 System.out.println("Printing plus " + i + " in a worker thread.");
12 try {
13 Thread.sleep(1000);
14 } catch(Exception e) {
15 e.printStackTrace();
16 }
17 }
18 }
19 });
20 thread.start();
21 // Loop running in main thread
22 for (int j = 0; j < 20 ; j++) {
23 System.out.println("Printing plus " + j + " in a main thread.");
24 try {
25 Thread.sleep(900);
26 } catch(Exception e) {
27 e.printStackTrace();
28 }
29 }
30 }
1class MultithreadingDemo extends Thread{
2 public void run(){
3 System.out.println("My thread is in running state.");
4 }
5 public static void main(String args[]){
6 MultithreadingDemo obj=new MultithreadingDemo();
7 obj.start();
8 }
9}
1public static void main(String[] args {
2 ...
3 Thread t1= new Thread(...);
4 t1.start();
5 ...
6}