• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Required help in thread

 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have URLReader class that read data from given URL and persist in database.



I have Datamanager class that call execute() of URLReader class for each element of Vector contain URL list.




My program is running fine with above logic as Java Application (running from command prompt).

But now a new requirement has come that we have to make this application multithreaded � as currently we have observed that our application is just using 30% of it resources and we want to leverage resources at the optimum level.

I believe one more class need to be develop that will be take no of current thread a command line parameter and decided Vector in to three Vectors and pass to each Thread

I am new to Thread.

How to achieve this?
Any other suggestions?
 
Manpreet Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to modify your DataManager class in a Thred !! e.g.

public class DataManagerThread extends Thread {

private String currentURL = "";

public DataManagerThread(Vector urllist){
//Create object of URLReader.
}

public void execute(){
//Iterate thru the Vector and call start() for each element.
DataManagerThread t = new DataManagerThread();
t.currentURL = "Current URL from Vector"
t.start();
}

public void run(){
// call URLReader.execute(t.currentURL)
}
}

P.S. Before instantiating these threads, you must check whether the URLs (in the Vector urllist) are unique,
so that you don't end up in a deadlock situation (Two threads reading the same resource )
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Manpreet for your reply.But you have not targeted issue that I have raised.

How can I pass number as command line arguments and that many numbers of threads will be created?
 
Manpreet Singh
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I pass number as command line arguments and that many numbers of threads will be created?

For this you would have to change the approach a little bit

In the main method -
1) Read the command-ling arguments (Say numberOfThreads)

2) Modify DataManagerThread

public class DataManagerThread extends Thread {

private Vector currentURLVector = null;

public DataManagerThread(Vector urllist){
//Create object of URLReader.
// Create Vector Array (Vector[] urllistArr) where -
a) size = urllist.size()/numberOfThreads + urllist.size)%numberOfThreads
b) Populate this Vector Array from urllist
}

public void execute(){
//Iterate thru the Vector[] urllistArr and call start() for each element.
DataManagerThread t = new DataManagerThread();
t.currentURLVector = urllistArr["Current Index"];
t.start();
}

public void run(){
//Iterate between URL's in t.currentURLVector
// call URLReader.execute(for each URL)
}
}

Hope this helps !!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the thread pools available since Java 5. You can create an Executor with a specified number of threads and queue up all your URLs for execution.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic