0

Why we have to create an instance of a class and attach it to the newly created thread object even if both are in the same class?

import java.io.*;
class thread1 implements Runnable{

   public void run(){
       System.out.println("thread started");
   }

   public static void main(String args[]) throws Exception{
      Thread t1=new Thread(new thread1());
      t1.start();
   }
}
akash
  • 22,664
  • 11
  • 59
  • 87
justin
  • 280
  • 3
  • 17

3 Answers3

1

You don't have to create a Runnable to perform custom code within a new Thread. It's also possible to create a subclass of thread directly.

public class WorkerThread extends Thread{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        // DO SOMETHING
    }

}

public class MainClass {
    public static void main(String[] args){
        new WorkerThread().start();

        MainClass mc = new MainClass();
        mc.startThread();
    }

    private void startThread(){
        Thread t = new WorkerThread();
        t.start();
    }
}
SME_Dev
  • 1,880
  • 13
  • 23
  • okay that's right.But why can't we create an instance of thread outside main function. – justin Oct 20 '14 at 11:33
  • You can. It was just an example, you can create and start a new thread anywhere. Just like you would instantiate any object. – SME_Dev Oct 20 '14 at 11:35
1

I think you have two questions in one:

1.) How to work with a Thread in Java? The answer of Fizer Khan is an example of this.

2.) How do static methods work in java? If you have a static method you are, in a maner of speaking, on a "static layer". You have no "this" reference because there is not object on this layer. Only if you create an instance you can access instance fields and non static methods on this object. If you add a second static method, you can do the same stuff as in your main method, because both are static. This is rudementary look at this question: https://stackoverflow.com/questions/18402564/how-do-static-methods-work

pulblic class Thread1 implements Runnable{ //name should be upper case

public void run(){
    System.out.println("thread started");
}

public static void main(String args[]) throws Exception{ //static method
   Thread t1=new Thread(new Thread1()); //t1 is a local reference to an object on the heap - no specil magic here
   t1.start(); //call to an "instance" method, can only be performed on an object.
}
Community
  • 1
  • 1
morpheus05
  • 4,772
  • 2
  • 32
  • 47
  • You cannot make an object static only a reference to it. You can have static methods. – morpheus05 Oct 20 '14 at 11:43
  • Thats fine, I mean, there is no reason not to do it. – morpheus05 Oct 20 '14 at 11:46
  • but i tried to do it but the error is:thread1.java:9: error: expected t1.start(); ^ – justin Oct 20 '14 at 11:47
  • then there is a syntax error or something, but I don't see it in your example. – morpheus05 Oct 20 '14 at 11:50
  • okay sorry I gave you the code where the thread was created and started in main().But I would like to see a program where thread is created and started outside main(). – justin Oct 20 '14 at 12:01
  • you cannot invoke thread without main static function. Why? Take a look http://stackoverflow.com/questions/8520497/compilation-error-identifier-expected – Fizer Khan Oct 20 '14 at 12:19
0

There are two ways to write threads.

public class ThreadX implements Runnable {
    public void run() {
        //Code
    }
}
/* with a "new Thread(new ThreadX()).start()" call */


public class ThreadY extends Thread {
    public ThreadY() {
        super("ThreadY");
    }
    public void run() {
        //Code
    }
}
/* with a "new ThreadY().start()" call */

public class MainClass {
    private Thread threadX = new Thread(new ThreadX());
    private Thread threadY = new ThreadY();

    public static void main(String[] args){
        // Call threads
        threadX.start();
        threadY.start();

        // some more threads
        new Thread(new ThreadX()).start();
        new ThreadY().start();
    }
}

When you extends Threads, You usually extend a class to add or modify functionality. So, if you don't want to overwrite any Thread behavior, then use Runnable.

Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • could you show me an example of creating and starting a thread object outside main function – justin Oct 20 '14 at 11:51
  • I added code to create instance of thread before main function as class memebers. – Fizer Khan Oct 20 '14 at 11:53
  • Because, you can instantiate the object as class member. But you need a main function to invoke it. http://stackoverflow.com/questions/8520497/compilation-error-identifier-expected – Fizer Khan Oct 20 '14 at 12:18
  • thanks that's what I was looking for.So is it possible to create a global thread using more than 1 class. – justin Oct 20 '14 at 12:28