• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread or Runnable ?

 
Jonny Kidd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all. I have written a java application, that java application then calls another Java application which then calls other Java applications, you still with me here ?
Basically it has 3 tiers. None of the tiers need to interact with each other, they are independent. I have made each application extend the Thread class.
The performance of the lowest threads (there's about 5 of them) is very poor though, basically they go off and connect to a website's API to grab data, then parse it and send it back. Because each application is independent, do I need to worry about the threads locking other threads still ?
Re-reading this, should I be looking at not using threads at all and just make all the applications entirely independent, or maybe use Runnable instead ?
Apologies if these questions are a bit vague, my Java is still very rough around the edges

Jon
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to run several applications in parallel, then threads are the way to go. It doesn't sound like they would interfere with each other, assuming the Java applications being called don't contend for the same resources. I'd suggest implementing Runnable instead of extending Thread (see here for some discussion), but functionally they're equivalent.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The basic idea of independent tiers is good. We often strongly suggest folks implement Runnable instead of extending Thread. It's more correct because you're not really writing new kinds of threads, but new kinds of tasks that can be run on threads. The conversion is very simple.

Once you're using Runnables we can do some very flexible things with Threads.

So, how do you call from one tier to another? Do you run multiple threads in each tier? Show some code where one calls another and let's see what we can do with it.
 
Jonny Kidd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys, I've changed the Thread's to Runnables now. I'll see how it goes.
 
Jonny Kidd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed my code yesterday but I still dont think I've got it right. I'll try and explain it a little further.
There are 3 tiers:

Class A --|
|--Thread B--|
| |--Runnable C
| |--Runnable C
|
|--Thread B--|
|--Runnable C
|--Runnable C

Hope that sort of makes sense.
Class A wakes up every hour and parses some data from an API source. Based on the data parsed it then creates Threads (B). These Threads then create Runnable's.
Each of the Runnables work independently of each other and there is no need for any interaction between the Runnables or their parent Threads.
Here are my main questions:
While Class A is sleeping, will this have any impact on the Runnables at the bottom of the tier ?
Should I be using Static methods and Variables for all my classes ?
It seems to work ok but intermittently nothing seems to be happening.

Thanks for any help/advice
Jon
 
Jonny Kidd
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, all the formatting went for my little diagram!
I'll try again:

Class A
|--------Thread B
|-------------------Runnable C
|-------------------runnable C
|
|--------Thread B
|-------------------Runnable C
|-------------------runnable C

Runnable's hang off of Thread B
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!