Lee Sigauke wrote: if I print out umber of threads left (totalThreads) it counts down but ultimately stops counting at 1027
Joanne
Lee Sigauke wrote:The way I see it is that the initial thread I make will remain alive and stay that way as its subsequent "children' continue to spawn more threads until they reach level 1 and when level one is reached the thread that is on level one dies and in doing so kills everything that it's connected to.
He're my attempt at doing so. I have also noticed that if I print out umber of threads left (totalThreads) it counts down but ultimately stops counting at 1027
another thing is that if I put my while loop condition to this.totalThreads == 0 it says non-static variable cannot be referenced from a static context but the variable is static already.
Steve
It stays alive until it has nothing left to do, regardless of what the state of threads it initiated is. In order to get the behavior you want you would need to build a communication channel that lets the threads at the lower levels tell the threads at a higher level that they are done, then you need to make the higher levels wait for the signal. You don't do any of this, so your top level threads end well before your lower level threads.
What, exactly, does 'this' mean? If you know the answer to that then the error makes complete sense.
Actually, that comment is totally wrong. That loop means 'keep spinning until totalThreads is not zero.' Since the new ThreadTree instance were the totalThreads count is set to 2048 is executed in the main thread before this while loop, this while loop never waits and will immediately complete and the main thread will end. You can prove it by putting this in the next line:
Next, your equation for the totalThreads is (int)Math.pow(2,level)*2). Why is that? What number do you expect to be the lower bound? Note, it isn't far from the number you posted. Why the extra *2?
Lee Sigauke wrote:That's what I thought as well and where I'm coming from is that this state of doing "work" is maintained for as long as the lower threads are still spawning children because this continuous spawn is still regarded at the work of the initial "parent" thread so if A spawns B spawns C spawns D spawns E spawns F (A will stay alive until F is reached where F is when we run out of code so as F runs out of code, E consequently runs out too and it propagates until it reaches A so the whole thing dies)
My understanding of 'this' is that it refers to the current instance of an object which is why I used it that way, my train of thought was that it if I'm mutating any variables then I will be changing those from the initial instance of the object.
My assumption is that since I'm making two more threads per thread, the initial thread will make two threads, each thread spawned from the initial thread will spawn 2^level so I multiplied that by two to get the total number of threads, unless I'm wrong. I did try to remove the *2 and it seems to cut off at 3, sometimes 2, sometimes 5 but never 1 or 0
Steve
You were calling from a static method. If 'this' refers to an instance, what instance would it refer to from a static method?
Right, 2^level already takes into account that each thread spawns 2 new threads (the 2 part of 2^level). So the extra multiplication by 2 is unnecessary. The reason you get a non-deterministic number (between 0 and 5 it looks like) is because of the synchronization problems I mentioned. Properly synchronized I was always getting 2 last night, and the countdown was correct if I extend the synchronization to the print statements. The reason it came to 2 was because the first Thread (level 10) was never taking itself from the pool (decrementing the value), so there was an offset by one. This would bring the count to 1. The reason it never reaches zero is a 'start counting at 0 or 1' thing. We know there should be 1024 threads based on the 2^10 formula, but are those thread ids 1-1024, or 0-1023. The assumption in the initial code is that counting starts at 1, but your while loop expects it to be 0. So you have to reconcile that by subtracting one from the total thread count at start, so everyone is expecting the same count: (2^level)-1.
Steve
Lee Sigauke wrote:
You were calling from a static method. If 'this' refers to an instance, what instance would it refer to from a static method?
A static method would be a class method thus if I use the keyword this it would be referring to a non existent instance because I have declared the variable as static which makes it a class variable as opposed to an instance variable, correct ?
The error wasn't about using totalThreads it was about using this.another thing is that if I put my while loop condition to this.totalThreads == 0 it says non-static variable cannot be referenced from a static context but the variable is static already.
Steve
Steve Luke wrote:I concur with Jayesh. I have done some research into Erlang for purely educational purposes. Although the cost of actually starting OS level threads is one thing, there is also the difficulty of writing good concurrent code that behaves as expected. Erlang seems to make it much easier than Java.
It is no measure of health to be well adjusted to a profoundly sick society. -Krishnamurti Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|