aspose file tools*
The moose likes Threads and Synchronization and the fly likes Required help in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Required help in thread" Watch "Required help in thread" New topic
Author

Required help in thread

Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
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?


My blood is tested +ve for Java.
Manpreet Singh
Greenhorn

Joined: Oct 13, 2006
Posts: 2
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

Joined: Sep 16, 2004
Posts: 3636
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

Joined: Oct 13, 2006
Posts: 2
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Required help in thread
 
Similar Threads
inputStream openStream
Memory leak with MySQL JDBC driver
how many initial connections should be opend
Bluetooth Chat application