dobse
dobse

Reputation: 35

Calling method of blocked thread

I'm just starting to learn about multithreading in Java, and am still figuring some things out. Firstly, can a class that extends Thread have other instance methods associated with it that can be called during its execution---and if so, can it change the state of the thread during its execution? Secondly, if this class is blocked waiting for a semaphore, can its instance methods still be called? Something like having these 2 threads run:

Thread1 t;
public class Thread1 extends Thread { 
    private int num;
    public run() {
        sem.acquire(); // here it blocks waiting for another thread 
                       //to call its setInt function and release it
        System.out.println("num is " + num);
    }
    public void setInt(int i) {
        num = i;
    }
}

public class Thread2 extends Thread {
    public run() {
        t.setInt(5);
        sem.release();
    }
}

Upvotes: 1

Views: 491

Answers (2)

HDJEMAI
HDJEMAI

Reputation: 9820

To demonstrate what you are looking for, here is the a code example wich I tested:

package test2;

import java.util.concurrent.Semaphore;

public class mainclass {

    static Thread1 t;

    static Semaphore sem;

    static Semaphore sem_protect;

    public synchronized static void main (String[] args) {

         sem = new Semaphore(0);

         sem_protect = new Semaphore(1);

         t =  new Thread1();

         Thread1 th1 = new Thread1();
         th1.start();

         Thread2 th2 = new Thread2();
         th2.start();

         try {
             synchronized (th2){
            th2.wait();
             }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

         System.out.println("The end !");
      }


    public static class Thread1 extends Thread { 

        private int num;

        public void run() {

            try {
                sem.acquire();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } // here it blocks waiting for another thread 
                           //to call its setInt function and release it
            try {
                sem_protect.acquire();
                System.out.println("num is " + num);
                sem_protect.release();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

        public synchronized void setInt(int i) {

            try {
                sem_protect.acquire();
                this.num = i;
                sem_protect.release();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


            System.out.println("value of num is: "+num);
        }
    }

    public static class Thread2 extends Thread {
        public void run() {
            t.setInt(5);
            sem.release();
        }
    }

}

Here is the result of execution of this code:

value of num is: 5
The end !
num is 0

With this result you can see that you can still access the methods of the class thread1 from the Thread2 . It means you access the method of the class instance, there is no method for a thread. (this is an answer for your first question)

The state of first thread is not changed by the second, num is still 0 for the first thread, the threads have each their own context.

even if we protect the access to num with another semaphore we dont have the same num value for the two threads.

Upvotes: 0

user207421
user207421

Reputation: 311050

There is some confusion here.

  1. Threads don't have methods. Classes have methods.
  2. Classes aren't blocked. Threads are blocked.
  3. You can call any method any time. The method itself may be synchronised, which will delay entry to it, or it may used synchronization internally, ditto, or semaphores, ditto.

Upvotes: 5

Related Questions