Home‎ > ‎Applied Core Java‎ > ‎Threads‎ > ‎

Program Without Thread

Problem : Create a program that does not implement Threads and experience sequential execution.

Program :

Step 1 - Coding


Create a text file c:\sunilos\HelloWithoutThread.java  and copy below contents.

/*
 * A program without a thread
 */

public class HelloWithoutThread {

    // Keeps Thread Name
    String name = null;

    // Constructor
    public HelloWithoutThread(String threadName) {

        name = threadName;
    }

    public void run() {

        for (int i = 0; i < 50; i++) {
            System.out.println(i + " Hello Thread " + name);
        }

    }

}

Step 2 - Creating Test Program


Create a text file c:\sunilos\TestWithoutThread.java  and copy below contents.


/*
 * A program to test HelloWithoutThread.java
 * copyright (c) sunilos Technologies Indore
 * @url : www.sunilos.co.in
 *
 */

public class TestWithoutThread {

    public static void main(String[] args) {

        HelloWithoutThread t1 = new HelloWithoutThread("Dhoni");
        HelloWithoutThread t2 = new HelloWithoutThread("Yuvraj");

        t1.run();
        t2.run();

    }

}

Step 3 - Deployment

  1. Create a folder 'c:\sunilos'. 
  2. Create or copy HelloWithoutThread.java and  TestWithoutThread.java into 'c:\sunilos' folder.
  3. Open your command prompt and go to  'c:\sunilos'
  4. Compile HelloWithoutThread.java and TestWithoutThread.java with help of
    •  javac HelloWithoutThread.java command.
    •  javac TestWithoutThread.java command. Compiled class file will be created in  the same folder.
  5. Congratulations!! your Test Java program is ready to serve.

Step 4 - Testing

  1. Make sure you are on Command Prompt under c:\sunilos directory
  2. Now start your  java program from command prompt with help of 
    • java TestWithoutThread  command.

Output


0 Hello Thread Dhoni
1 Hello Thread Dhoni
2 Hello Thread Dhoni
.......
.......
.......
46 Hello Thread Yuvraj
47 Hello Thread Yuvraj
48 Hello Thread Yuvraj
49 Hello Thread Yuvraj

Conclusion


In normal case (without thread), until first statement t1.run()execution is finished control can not be transferred to next statement t2.run(). That shows sequential execution.

We will have different experience in case of Threading. Now time has come to experience that, please click Next link without any delay...

<<Previous | Next>>
ċ
HelloWithoutThread.java
(0k)
Mahima Hardia,
Aug 19, 2011, 2:32 AM
ċ
TestWithoutThread(1).java
(0k)
Mahima Hardia,
Aug 19, 2011, 2:39 AM
Comments