37. Explain different ways of creating a thread

Threads can be used by either :

  • Extending the Thread class
  • Implementing the Runnable interface.


class Counter extends Thread {

// method where the thread execution will start

public void run() {

// logic to execute in a thread

}

// let’s see how to start the threads

public static void main(String[] args) {

Thread t1 = new Counter();

Thread t2 = new Counter();

t1.start(); // start the first thread. This calls the run() method

t2.start(); // this starts the 2nd thread. This calls the run() method

}

}

class Counter extends Base implements Runnable {

// method where the thread execution will start

public void run() {

// logic to execute in a thread

}

// let us see how to start the threads

public static void main(String[] args) {

Thread t1 = new Thread(new Counter());

Thread t2 = new Thread(new Counter());

t1.start(); // start the first thread. This calls the run() method

t2.start(); // this starts the 2nd thread. This calls the run() method

}

}

The runnable interface is preferred, as it does not require your object to inherit a thread because when you need multiple inheritance, only interfaces can help you. In the above example we had to extend the Base class so implementing runnable interface is an obvious choice. Also note how the threads are started in each of the different cases as shown in the code sample.


6 comments:

  1. So in conclusion, the answer is no, a free reverse cell phone lookup does
    not currently exist. When one mentions about a phone, he or she may
    be talking about either the telephone or the mobile version or the cellular phones.
    In the last few years, you may have heard the words bullying or cyberstalking in
    the news quite a bit.

    My web page - phone directory

    ReplyDelete
  2. Plans for visiting Universal Studios or Disneyland as part of the trip may make you decide to see
    Six Flags Magic Mountain at the outset of the trip

    Look into my web site: six flags fiesta texas coupons san antonio

    ReplyDelete
  3. Some, however, will try to take over the garden bed. After
    all, they can be readily sourced from the internet.
    After throughly washing the area then placing ice on
    top of acne will support with the swelling and pain.

    Feel free to surf to my web-site - how to get rid of warts asap

    ReplyDelete
  4. An external thrombosed hemorrhoid generally develops
    over time. And sometimes if the hemorrhoid is already
    severe bleeding occurs. An effective cure for hemorrhoids, especially a conventional one, can help you manage the condition well.


    Take a look at my blog :: internal hemorrhoids treatments

    ReplyDelete
  5. This is because you can rely on assistance from Family Lawyers Parramatta and involving various other court personnel.

    Because the civil laws in particular have developed over
    hundreds of years, they have become highly complex.

    Mentor - In the newspapers I looked for any opportunity that would
    allow me take control and improve my unbelievable life.

    ReplyDelete
  6. The child support is intentionally for the children's education, clothing, food, school supplies, and other necessities the youngster should live by. A legal situation regarding custody, divorce, divorce, and other family related legal issues may maybe not be as easy because it seems. Debts and assets might talk about what the couple has, including all securities, shares, property, cars, and current debts.

    Look at my web blog: com.au

    ReplyDelete