Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Example in java
12-01-2009, 07:03 AM (This post was last modified: 12-01-2009 07:28 AM by Back_track.)
Post: #1
Thread Example in java
sourcecode is attached at the bottom javac Main.java to compile and java Main to run

This is the main class

Code:
public class Main{
    public static void main(String[] args){
        MyThread tt = new MyThread(); //initialize thread class
        tt.start(); //start the thread
        for(int x = 0; x < 20; x++) //loop through main program
            System.out.println("Main thread running");
    }
}

This is the thread class
Code:
public class MyThread extends Thread{ //extending the thread class
    public void run(){  //run is a interface that must be override in the thread class you put everything you want in here that you want to run in the thread
        for(int x = 0; x < 20; x++){ //loop through the thread
            System.out.println("New Thread running");
        }
    }
}


Attached File(s)
.zip  thread example.zip (Size: 2.69 KB / Downloads: 0)
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: