Extending the Thread class Assignment Help

Assignment Help: >> Creating threads in Java >> Extending the Thread class

Extending the Thread class:

When using threads, the general approach is first to define a class by extending the Thread class and overriding the run method. In the run method, you should write the code that you wish to run when this particular thread has access to the CPU. Once you have defined your thread class, you can then create an instance of it and set the instance running using the start method that is defined in Thread.

An example of a thread definition, called WhereAmI, using inheritance from the Thread class is shown below:

public class WhereAmI extends Thread
{
int thisThread;

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

// override the run method

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

So we might create and use an instance of this class as follows:

// create an instance of WhereAmI

WhereAmI example = new WhereAmI(2);

/* Invoke the run method of WhereAmI using the start method of Thread. */

example.start();

In the case above, this code will print out 100 times that it is thread 2 that is currently running.

From your knowledge of programming so far, this will seem to be a fairly straightforward piece of code - when an instance of this class is created and the start method is invoked then you would expect it to print to the screen 100 times the string "I'm in thread 2" (or whatever value was passed to the constructor). However, as we shall see shortly, this may not be the case.

An example involving three threads is shown below:

public class ThreadTester
{
public static void main (String [] args)
{
// create the threads
WhereAmI place1 = new WhereAmI(1);

WhereAmI place2 = new WhereAmI(2);

WhereAmI place3 = new WhereAmI(3);

// start the threads place1.start();

place2.start(); place3.start();
}
}

The class ThreadTester creates three instances of the WhereAmI class. Each instance is passed a different value so that we can identify which thread is running at any particular time. So the first three lines of code in main create the three threads. The next three lines of code call the start method of each thread to invoke the run method of each Thread object. The run method is never invoked directly by the programmer, but is invoked by the Java run-time system. You might have expected that the code in the place1 instance would run first, followed by place2 and then place3. A segment of a typical output is given below:

I'm in thread 3

I'm in thread 1

I'm in thread 3

I'm in thread 1

I'm in thread 3

I'm in thread 1

I'm in thread 3

I'm in thread 2

I'm in thread 3

I'm in thread 2

I'm in thread 3

I'm in thread 2

We can see here that no one thread had exclusive access to the CPU for its entire execution. One thread has access for a certain amount of time and then another thread has access, and so on. What you will also see is that it is not the case that the threads
take it in turns to run

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your Extending the Thread class homework and assignments? Live Extending the Thread class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Extending the Thread class homework help, java assignment help and Extending the Thread class 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