• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Make a thread run concurrently instead of one after another

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following code, which is used to watch a directory and on a creation of a file a thread starts, it should read a CSV file and save the values of it in an object, after that I use the data saved in the object to sort the data from the CSV file in new directory depending on some strategies defined in a config.proprties file, when I paste some files in the directory that is being watched, it gets processed one after another, but what I want is to make them run at the same time unless I have more than one file that will use the same strategy can someone advice what am I doing wrong?

Here is my Watcher class:


And here is the class that used to read the property file and match it with file that have been created:

And here is my strategy implementation class:

When I run the code I get the following result:

Thread Name = Thread-2
Thread Name = Thread-2
Thread Name = Thread-2
Thread Name = Thread-2
Thread Name = Thread-2
Thread Name = Thread-5
Thread Name = Thread-5
Thread Name = Thread-5
Thread Name = Thread-5
Thread Name = Thread-5
Thread Name = Thread-4
Thread Name = Thread-4
Thread Name = Thread-4
Thread Name = Thread-4
Thread Name = Thread-4
Thread Name = Thread-3
Thread Name = Thread-3
Thread Name = Thread-3
Thread Name = Thread-3
Thread Name = Thread-3
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think perhaps if the work the threads are doing took a lot longer, you might get them running at the same time. Maybe put some Thread.sleep calls in there to slow them down? I don't see anything in there which would prevent those threads from starting and doing their work right away.
 
khaled abu shamat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul my validatePatternAndCheckCase method is synchronised
 
khaled abu shamat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:I think perhaps if the work the threads are doing took a lot longer, you might get them running at the same time. Maybe put some Thread.sleep calls in there to slow them down? I don't see anything in there which would prevent those threads from starting and doing their work right away.

my validatePatternAndCheckCase methos is synchronised
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

khaled abu shamat wrote:Paul my validatePatternAndCheckCase method is synchronised



I see. And you did that because if two threads accessed the method at the same time then it would cause problems?
 
khaled abu shamat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

khaled abu shamat wrote:Paul my validatePatternAndCheckCase method is synchronised



I see. And you did that because if two threads accessed the method at the same time then it would cause problems?



I will be a problem as long as the files that being passrd will get a different strategy but if they will do so that will be a problem if you like i can show more code
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Couldn't you just create a new PropertiesValue in each new thread?
It doesn't look like you need to store any results in it from the various runs.
 
khaled abu shamat
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:Couldn't you just create a new PropertiesValue in each new thread?
It doesn't look like you need to store any results in it from the various runs.



What i have is a class that has a singleton design pattern to create a new instance of the strategy
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

khaled abu shamat wrote:What i have is a class that has a singleton design pattern to create a new instance of the strategy



Okay. And why is it necessary to have a singleton?
 
Honk if you love justice! And honk twice for tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic