d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Multithreading Java
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jul 24 2012 03:51pm
So here's a basic overview of what's going on, overall I'm basically processing order forms:

Classes:
Customer class - contains customer name + list of items ordered
Item class - contains item name and price
Manager class - contains all the methods below + a main method that runs the program

method 1: converts .txt file to a new customer object and a list of items purchased (TreeMap made up of <itemName, amountBought>)
method 2: converts .txt file containing all possible items and their prices to a list of item objects
---each item object has a name and price

method 3: Creates a string containing all the customers purchase information in order
method 4: Creates a string containing the summary of all customer purchases combined

Method 5: Writes the data (from method 4 and 5) to a .txt file of my choosing

My question:

I'm trying to implement multi-threading so that each customers info can be completed at the same time, instead of completing them in sequential order.

(IE: order1, order2, order3...ect) to (all orders at once)

I guess I'm asking if my code fails to take advantage of multi-threading, as the only method each individual order calls is method 1.

This post was edited by lopelurag on Jul 24 2012 03:53pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jul 24 2012 03:57pm
The code has to be implemented to allow it to run with a single thread (I assume which just means running the main method) and multi-threading (one thread will be created for each order to be processed)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 24 2012 04:30pm
Quote

I guess I'm asking if my code fails to take advantage of multi-threading, as the only method each individual order calls is method 1.


is this your question? you didn't post your code so i can't tell you if your code uses multiple threads.

or are you asking how to do the hw?

i assume you dont want an actual producer/consumer model (one thread reads data into a queue, other thread processes them and puts it back into file)? it sounds like you want the main thread to read data into a queue and use a thread pool to use separate threads to process each order. be sure to use the pool! you don't want to kill the performance by creating 1000 threads if only 2 are needed. the IO is so short that each thread will finish very quickly.
Member
Posts: 299
Joined: Apr 11 2010
Gold: 2,491.00
Jul 24 2012 04:33pm
Hard to say with as little as you've posted but basically:

To do method1 without multithreading, you'd just make a function that reads the .txt and generates a single customer, and then call that method from a loop of some sort for each .txt file.

To do method1 with multithreading, you might implement a FutureTask that is constructed with a filename, reads that file and produces a Customer during execution, and results in a Customer object. Then you could spawn many of these futures and collect the results when they've completed.

This is just theorycraft; if you want something more concrete I'd need more detail.
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jul 24 2012 05:00pm
Quote (PumblesMumbles @ Jul 24 2012 06:33pm)
Hard to say with as little as you've posted but basically:

To do method1 without multithreading, you'd just make a function that reads the .txt and generates a single customer, and then call that method from a loop of some sort for each .txt file.

To do method1 with multithreading, you might implement a FutureTask that is constructed with a filename, reads that file and produces a Customer during execution, and results in a Customer object. Then you could spawn many of these futures and collect the results when they've completed.

This is just theorycraft; if you want something more concrete I'd need more detail.


Yeah I guess my question is more theory based.

My code is supposed to be able to run a single thread (taking in multiple .txt files such as file1.txt up to file2000.txt) and output a summary of each order and total summary of all orders to a designated .txt file.
It's also supposed to be able to use multithreading so that one thread is created for each order to be processed.

Classes:
Manager - http://pastebin.com/n2bFwNmh
Customer - http://pastebin.com/zT8j1CYi
Item - http://pastebin.com/r8kYtaS1

I just feel as though the only aspect of my code that would improve in respect to run-time efficiency when using multi threading vs a single thread is when reading in the .txt files.

Other than that most of my output code is completed in one go once all the orders are read in.

This post was edited by lopelurag on Jul 24 2012 05:05pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jul 24 2012 07:12pm
Nvm took a while but I figured it out :P
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll