-4

I want to create a class that IS A TimerTask and also a Thread. I am assuming that there is nothing wrong with this idea. The reason why I am doing it is mentioned in the "desired output" section of my post here.

Java class CANNOT inherit from two classes. So, how do I fix this problem?

The reason is in the post i mentioned in my question.

Actual output: All timer tasks are executed in the main method. My main code's last print statement is displayed before all the timer tasks are executed. I don't want that.

Expected output: I want the end print statement of main to come after everything else.

Community
  • 1
  • 1
Time
  • 1,551
  • 2
  • 13
  • 14
  • 2
    "I want to create a class that IS A TimerTask and also a Thread" Why? – Cephalopod Mar 11 '13 at 09:09
  • @Arian - The reason is in the post i mentioned in my question. All timer tasks are executed in the main method. My main code's ending print statement is displayed before all the timer tasks are executed. I don't want that. I want the end print statement of main to come after everything else. – Time Mar 11 '13 at 09:13
  • You should have framed a different question for your problem than questioning the design logic – rajesh Mar 11 '13 at 09:15
  • Why dont you read more closely the comments on your own answer on your original post. – vikingsteve Mar 11 '13 at 09:16
  • @vikingsteve - i read it. But, I don't want to create another task just to exit the main program and remember all the necessary timings. I want the power of join() also. Is that okay ? – Time Mar 11 '13 at 09:20
  • You can't inherit more than a class but you can implement as many interfaces as you want. – Rob Mar 11 '13 at 09:21
  • @Rob - yes, i know that. but it cannot be used here - TimerTask itself implements Runnable. I want TimerTask to also have the join method of thread class. This seems impossible. – Time Mar 11 '13 at 09:23
  • Why wouldn't you simply create a class that extends `TimerTask`, adding the `join` method you want and inherit this class? In any case it will be useless to implement `Runnable`. – Rob Mar 11 '13 at 09:25
  • Sorry I forgot to mention I want Thread join() method. – Time Mar 11 '13 at 09:27
  • @Time Yes, there most certainly is something wrong with the idea of extending two classes in Java: it is not supported. Why do you even ask, when you yourself say that Java does not support multiple inheritance? – hyde Mar 11 '13 at 09:30
  • @hyde - the reason for my asking is mentioned in the question. – Time Mar 11 '13 at 09:33
  • 1
    @Time Attempt at constructive feedback: You ask as if no MI is the problem, and you want workaround for *that*. Instead, you probably should ask something like "Alternative to TimerTask, which supports waiting for completion?", and Jeremy has a very good answer for that below. – hyde Mar 11 '13 at 09:51

4 Answers4

9

Extend TimerTask and Implement Runnable

Edit made after restating original problem: Check https://stackoverflow.com/a/4951059/869488

Community
  • 1
  • 1
rajesh
  • 3,247
  • 5
  • 31
  • 56
  • 9
    `TimerTask` itself implements `Runnable`. So this is meaningless. – uba Mar 11 '13 at 09:10
  • 1
    Yes I was checking that now. Anyway I typed in the first thing that came to my mind before checking the docs :) – rajesh Mar 11 '13 at 09:11
  • How does this get 7 upvotes while my question gets 3 downvotes ? unfair :( – Time Mar 11 '13 at 09:16
  • 3
    @Time 3 down votes may be for any reason like your title 'Design flaw of the Java Language', its not constructive, question format etc. There are 8 upvotes here for simplicity of the reply. – Jayamohan Mar 11 '13 at 09:22
  • 2
    @Time : your question is a simple one but the answer was very simplified , so it gets vote , besides , here we are to get answers to our questions , not the upvotes – Hussain Akhtar Wahid 'Ghouri' Mar 11 '13 at 09:36
5

You're asking the wrong question. Multiple inheritance isn't possible in Java, but that's not your actual goal.

What you want is the ability to wait for multiple timed tasks to finish. There are a few ways of achieving that.

Probably the most straightforward is using a ScheduledExecutionService, which will give you a Future that you can wait on, in a way similar to the join method you're used to.

Another way would be to use a CountDownLatch to have each timer task decrement the latch when it is done; you can then wait for them to finish by using await.

Yet another is the strategy of using the wait and notify primitives to build the synchronization yourself – your problem isn't hard to address this way, either.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
  • Thinking of using ScheduledExecutorService. But, looks like there is an issue - http://code.nomad-labs.com/2011/12/09/mother-fk-the-scheduledexecutorservice/ Not so sure now. – Time Mar 11 '13 at 10:16
  • What sort of exceptions do you expect? If they should not cause the task to be cancelled, then you should handle them within your task. If not, they will be re-raised as an `ExecutionException` when you `get` the `Future`. As far as stopping subsequent executions, that only happens if an uncaught exception is thrown. I'm not sure I see the problem. – Jeremy Roman Mar 11 '13 at 15:13
2

The design you want is not supported by Java.

A supported design is to use wait and notify instead of joining to the TimerTask thread (which you can't do). Examples here under this SO question: A good small example to demonstrate wait() and notify() method in java

Community
  • 1
  • 1
hyde
  • 60,639
  • 21
  • 115
  • 176
1

You can implement Runnable.

class BulbJob extends TimerTask implements Runnable
{

    public void run()
   {

   }
}
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
Joe2013
  • 1,007
  • 1
  • 9
  • 24