Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Look Inside Java : Thread created by Runnable

  Asked By: Gin    Date: Nov 13    Category: Java    Views: 880
  

I have create the code to create Thread by
Runnable interface. I have created my own Interface & Class to create Thread
and I feel to share my knowledge among us.

SUBJECT : Create Thread Using Runnable Interface

One of the ways to create Thread in Java is using Runnable Interface. Here I
want to clear that whenever I use Create Thread it explicitly mean to create
Thread by Runnable Interface.

Here I am create My own Class & Interface to Create Thread with prefix 'N' to
Class or Interface name than that of actual name in Java.
For Example. class Thread = class NThread

Here below I have demonstrated what actually java coding to create Thread.

INSTRUCTIONS:

Here all constructor & methods that I have implemented are same java has
implemented but I am implement only those things which is require to accomplish
the task.

Put all these three program of Java in same directory otherwise it require
classpath to set.


(1)
************************************************
NRunnable Interface Instead of Runnable
************************************************

public interface NRunnable // 'N' Prefix
{
public void run();
}
-----------------------------------------------------------------------------


(2)
************************************************
NThread Class Instead of Thread
************************************************

/*
FIELD
1. target is an object of NRunnable Interface

CONSTRUCTOR
1. NThread(NRunnable target)

METHODS
1.void run()
2.void start()
*/

public class NThread implements NRunnable
{
private NRunnable target;

public NThread(NRunnable target)
{
this.target = target;
System.out.println("From NThread(NRunnable target)...");
}
public void run()
{
/*
if we use constructor NThread(NRunnable target) while creating Thread at that
time in target object we assign something which we have pass in constructor.
here argument is object of class that implements NRunnable. So at that time
target is not null and below condition become true and target.run() called the
method of the object which we have passed in constructor means it called the
method of MyThread class
*/
if(target!=null)
target.run();
else
System.out.println("From NThread.run()...");
}

public void start()
{
System.out.println("From NThread.start()...");
System.out.println("Calling run()...");
run();
}
}//class over

-----------------------------------------------------------------------------


(3)
*********************************************
MyThread Class (User Created)
*********************************************

// User class which implements NRunnabel
/*
METHODS
1. void run()
*/

public class MyThread implements NRunnable
{
public void run()
{
System.out.println("From MyThread.run()...");
}

public static void main(String args[])
{
System.out.println("From MyThread.main()...");
MyThread mt1 = new MyThread();
/*
note the argument passed while createing NThread object. It is object of
MyThread class
*/
NThread nt1 = new NThread(mt1);
nt1.start();
}
}// class over
-----------------------------------------------------------------------------


Compile all the program and then run MyThread class
Think on output and compare it with code...follow the execution of code...

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on Look Inside Java : Thread created by Runnable Or get search suggestion and latest updates.




Tagged: