Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make a Thread run for particular amount of Time

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I want to start a Thread , that actually runs for one minute and stops after it .


Please let me know how to achive this . Thanks .
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can join on a thread for some time and after the join if it still is alive, interrupt to kill it.

Your thread must be able to handle the interruption that is going to occur.
This is one of the possible ways... may be someone else will come up with some more possibilities
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can join on a thread for some time


Thanks for your reply , But do this operation , there should be two threads .

Right now , a normal java class is calling this Thread.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Kiran V wrote:But do this operation , there should be two threads .

Right now , a normal java class is calling this Thread.

Non-sequitur?
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non-sequitur?


what do you mean by above ??
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Non_sequitur_(logic)
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, I come to the same conclusion.

The current thread starts this new thread. It should not block afterwards. However, this new thread needs to be limited in time. Using join on this thread from the current thread will block the current thread. To prevent that, the current thread starts two threads:
- the timed thread
- a thread that will call join on the timed thread
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:To be honest, I come to the same conclusion.

The current thread starts this new thread. It should not block afterwards. However, this new thread needs to be limited in time. Using join on this thread from the current thread will block the current thread. To prevent that, the current thread starts two threads:
- the timed thread
- a thread that will call join on the timed thread

2 threads - thats really nice! Good one,Rob!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic