• 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

Understanding threading

 
Greenhorn
Posts: 3
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am trying to understand the concept of threading a bit more

The idea was, to create a random function which stores the generated values in a DB.





Why do threads numbers (atleast in my case) start with 9 and up?
Why does the while(Thread.activeCount()>1) then wait for all the threads to finish doing their work before closing the DB connection?
I'm not quite happy with the way I disconnect from the DB (waiting for the while function to exit) - any suggestions on how to make that piece a bit better?

Is there a new instance of handleDB() created in every thread, or is there only one used throughout the application? (is there a way of seeing it myself?)

Why does counter (if written in a separate file) return random numbers? I'd like it to return the ammount of times an entry has been made in the DB. If I wrote the code in the same file where I have my main, I could use the static class modifier and it worked - in a separate file it doesnt :/


 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Mike Skruf wrote:Why do threads numbers (atleast in my case) start with 9 and up?


There are a number of threads outside your control that the JVM uses for things (like initialization, garbage collection, and other things).

Why does the while(Thread.activeCount()>1) then wait for all the threads to finish doing their work before closing the DB connection?


You shouldn't count on that working... it isn't the correct way to do things. The safe way would use some form of inter-thread communication to signal the work is done.

I'm not quite happy with the way I disconnect from the DB (waiting for the while function to exit) - any suggestions on how to make that piece a bit better?


1) Put the work in a try {} clause
2) Add some form of inter-thread signalling to determine when all your threads are done. (see this tutorial for some ideas on how). This would be done inside the try clause
3) Disconnect the DB in a finally{} block after the try{}.

Is there a new instance of handleDB() created in every thread, or is there only one used throughout the application? (is there a way of seeing it myself?)


The easiest way to see for yourself is to read the code. Where is new handleDB() called? How many times does it get called? How is/are it/they used?

If you can't figure it out then you could add some things to help:
- System.out.println() the handleDB used in each thread. Look at the output to see if each thread puts out the same thing.
- Use a profiler

Why does counter (if written in a separate file) return random numbers? I'd like it to return the ammount of times an entry has been made in the DB. If I wrote the code in the same file where I have my main, I could use the static class modifier and it worked - in a separate file it doesnt :/



It isn't because you moved where the class is, it is because you are changing the value of the counter in an unsafe manner. Read that tutorial I gave you.
 
Mike Skruf
Greenhorn
Posts: 3
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve, thank you for your prompt answer(s).
I went through your post and the tutorial you provided several times (to avoid making stupid replies). I managed to solve the counter problem (using synchronized), but I am still having slight issues with dbHandler.
Maybe it's the language barrier, but I couldn't wrap my head around this.


What I want to do is
in main establish a connection to DB (by making an instance of handleDB)

spawn a couple of threads that generate values
have this threads store values in the DB through the already established instance of handleDB
close threads

Now, achieving it - it's another story


 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Skruf wrote:Now, achieving it - it's another story


That's true. What you should do next is start to write out in detail about what you want to do. Do it with pencil and paper, and write in your native language - not in Java. Then, once you have a description, write out how to do each step - again in your native language - in more detail. Repeat the process, providing more and more detail until each thing to be done is broken down into discrete steps that can't be made any simpler. At this point writing the Java code should be fairly straight forward since you will have a good understanding of what needs to be done, start to finish.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic