0

Lets say I have a class that contains a list of sorted numbers.

The object has a get function so i can see the private variables inside the class (in this case, the array)

If I thread the object, how do I access the class get function?

eg
Thread t1 = new testclass();
t1.start();
t1.getvalue() ; ??

I haven't coded anything yet, still in the design phase of my assignment

4 Answers 4

3

Normally, in Java, people use Runnable instead of deriving from Thread:

class MyThing implements Runnable {
    private int x;
    public void run() {
        x = 10;
    }
    public int getX() { return x; }
}

MyThing thing = new MyThing();
Thread t = new Thread(thing);
t.start(); // The thread starts and calls MyThing::run() in itself
t.join(); // wait for t to finish
System.out.println(t.getX());

There isn't anything special about a Runnable aside from the interface -- you could just as easily call thing.run() directly, except it would run in your own thread.

Sign up to request clarification or add additional context in comments.

2 Comments

EJSE, Item 68: Prefer executors and tasks to threads. Instead of doing Thread t = new Thread(thing);t.start();t.join(); do this: Executors.newSingleThreadExecutor().submit(thing).get()
I did that way and didn't work. t don't recognize my method declared in MyThing.
2

You wanted a Runnable/Thread that can give you a result after some work is done?

I recommend that you replace Thread/Runnable with Callable, and submit it to ExecutorService.

    java.util.concurrent.Callable c = new java.util.concurrent.Callable() {

        @Override
        public Object call() throws Exception {
            int result = 0;

            // e.g.
            // 1. do some work here
            // 2. update result

            return result;
        }

    };

    java.util.concurrent.ExecutorService threadpool
            = java.util.concurrent.Executors.newSingleThreadExecutor();

    java.util.concurrent.Future future = threadpool.submit(c);

    Object result = null;
    try {
        result = future.get(5, java.util.concurrent.TimeUnit.SECONDS);
    } catch (Exception e) {
        System.err.println("ERROR!");
        e.printStackTrace();
    }

    System.out.println("RESULT : "+result);

Comments

0

Not quite.

testclass t1 = new testclass();
t1.start();
t1.getvalue();

3 Comments

correct me if im wrong but isnt that just normla class accessing ? I am planning to thread the objects
So what? That the class is threaded is an internal implementation detail. Callers shouldn't have to worry about it. (If they did, how would you change a class from threaded to not threaded or vice versa without changing all the code that accessed it?)
I agree with @DavidSchwartz here. Any locking/queueing/waiting/whateverSignaling should happen inside the getter so that any old thread can call it from anywhere are get a valid result.
0

Assuming that other syntax are correct, your code will not compile because t1 is an object of Thread class and the Thread class has no method getvalue(). You can't call the methods of testclass directly from the thread object.

Once you call the t1.start(), the run() method of the testclass will be executed. So if you want to call the getvalue(), you should call it within the run() method and do whatever you want there or you should call the getvalue() method from an object of the testclass.

Inside the run method of the testclass:

public void run() {
    //Do whatever you want to when the thread starts here.
System.out.println(Thread.currentThread().getName());
    this.getvalue(); //This will call the getvalue() method of the test class.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.