wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Understanding threading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Understanding threading" Watch "Understanding threading" New topic
Author

Understanding threading

Mike Skruf
Greenhorn

Joined: Oct 27, 2013
Posts: 3

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 :/


Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.


Steve
Mike Skruf
Greenhorn

Joined: Oct 27, 2013
Posts: 3

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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Understanding threading