Implementing the Runnable interface Assignment Help

Assignment Help: >> Creating threads in Java >> Implementing the Runnable interface

Implementing the Runnable interface:

Defining threads by extending the Thread class is satisfactory if you wish to inherit from only one class. However, for many applications you will want to inherit from another class as well as from Thread. As Java does not allow multiple inheritance, there is an alternative approach to defining a thread, using the Runnable interface. This is an interface that contains just one method, run. To create the WhereAmI thread that we created previously using the Runnable interface all we need do is write:

public class WhereAmIRun implements Runnable
{
int thisThread;

// constructor
public WhereAmIRun (int number)
{
thisThread = number;
}

// implement the run method public void run ()
{
for (int i = 0; i < 100; i++)
{
System.out.println("I'm in thread " + thisThread);
}
}
}

The code above is identical to the code used earlier, except that rather than extending the Thread class we are now implementing the Runnable interface, which contains just the signature of the run method. As there is only the signature of run in the Runnable interface when we add the code to run, we are implementing the method rather than overriding it as we were before.

The Runnable interface does not contain a start method and so we have an extra step to perform before we can use the class to create a thread. To create a thread from this object, we need to first create an instance of WhereAmIRun and then pass it to the Thread class constructor. This will create a thread with a start method that can be used as before to activate it.

public class ThreadTesterRun
{
public static void main (String [] args)
{
// create a runnable object

WhereAmIRun place1 = new WhereAmIRun(1);

// now create a thread to run it

Thread thread1 = new Thread(place1);

// repeat for two further objects

WhereAmIRun place2 = new WhereAmIRun(2);

Thread thread2 = new Thread(place2);

WhereAmIRun place3 = new WhereAmIRun(3);

Thread thread3 = new Thread(place3);

// start the threads thread1.start();


thread2.start();
thread3.start();
}
}

When run, this code will produce the same sort of result as with the thread created by extending Thread.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Implementing the Runnable interface homework and assignments? Live Implementing the Runnable interface experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Implementing the Runnable interface homework help, java assignment help and Implementing the Runnable interface projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd