Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Ted North

Ranch Hand
+ Follow
since Jan 02, 2012
Ted likes ...
Python Java
Cows and Likes
Cows
Total received
1
In last 30 days
0
Total given
0
Likes
Total received
8
Received in last 30 days
0
Total given
65
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ted North

Congratulations! That is incredible to have so many posts.
5 years ago

Yin Stadfield wrote:

Ted North wrote:"A static member can call/access only a static member of its own class" (Ganesh & Sharma, 2013, p. 488). Does this mean that a static reference can only call methods in its class or call fields in its class using the name of the class and the dot operator?



Hi Ted,

If I understand your statement correctly, I'll say this is not the case: Does this mean that a static reference can only call methods in its class or call fields in its class using the name of the class and the dot operator?
Because if it is within the class itself, you won't be needing to use the class name + dot operator. Example:


You can't however, call an instance members from a static reference:



Interesting reply. Thank-you for thinking about this some with me. I think in your last example with source-code you meant to assign instanceInt1 to staticInt3 to show the compiler error. Also, I think you meant to assign Example1.instanceInt1 to staticInt2 to show a compiler error. I see what you meant though. I forgot about how it is a compiler error to assign a instance variable to a static variable.

In my opinion calling a static member without a reference of any kind, class reference or object reference, is outside of the scope of this question. Unless in the statement in question a 'static member' is a method that calls instance members in its own class or a different class after creating instances of the class so that the instance members can be called from a static context.

Wow, this can become confusing.

Thank-you for the help!

Regards,

Ted

Sergej Smoljanov wrote:I think this assume word access directly .
you can access static members of other class use type name and dot and name of types static member. and also you can create instance of class and access non static member of instance



Your reply is confusing to me. The last sentence on creating an instance and accessing instance members makes perfect sense to me though!
Hi All,

I am confused by this statement, "A static member can call/access only a static member of its own class" (Ganesh & Sharma, 2013, p. 488). Does this mean that a static reference can only call methods in its class or call fields in its class using the name of the class and the dot operator?

Respectfully,

Ted

Reference

Ganesh & Sharma. 2013. OCP Java SE 7 Cert Guide. Apress.
I am still not sure how fork() ends up calling ForkJoinWorkerThread.execTask() that results in compute() being called again. I think this functionality has something to do with the mysterious pushTask() method I was able to locate at grepcode.com in the openjdk version 7 of Java here: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7u40-b43/java/util/concurrent/ForkJoinWorkerThread.java#ForkJoinWorkerThread.pushTask%28java.util.concurrent.ForkJoinTask%29

However, looking at the Oracle documentation for the class ForkJoinWorkerThread there is no mention of the strange pushTask() method. Here is the URL for the Oracle API documentation on said class: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinWorkerThread.html

The pushTask() method does some complex bit shifting I think so it is difficult for me to understand. Does anyone have a layman's definition of what the pushTask() method does? I am thinking something along the lines of the excellent Head First Java by Sierra and Bates.

If pushTask() does not call the execTask() method what actually does? Finally, why is pushTask() shown in the Open-JDK 7 but not in the Oracle proprietary JDK?

Regards,

Ted

Ankit Garg wrote:Directly the fork() method doesn't call the compute() method, fork() submits the task to the worker pool. You can check the source code

To backtrack, I think you can check ForkJoinWorkerThread.execTask method, which calls ForkJoinTask.doExec which calls RecursiveTask.exec where compute() method is called



Wow! Thank-you for the helpful reply Ankit. I can follow your logic in how compute() is called again by fork() but I am confused about how ForkJoinWorkerThread.execTask() gets called and starts the process that eventually ends with compute being called again. Does it have something to do with the pushTask() method inside the fork() method? I searched for pushTask() on DuckDuckGo and on GrepCode but could not find where this method is defined.

Thank-you for sharing GrepCode.com. That is a convenient way to see the source-code of Java itself.

Respectfully,

T

Ankit Garg wrote:

Ted North wrote:My question is: What is the fork() method doing in the program? Is it recursively calling the compute() method or is it summing up the numbers without making sure the value is not too large in the first if statement in the compute() method. That if statement is: if ( (to - from) <= N/NUM_THREADS)


Hi Ted, the fork method calls the compute method asynchronously. The recursive behaviour is defined in the compute method itself...



Thank-you for the reply Ankit. What you are saying makes sense, that fork() calls compute() again at some later time in some different thread.

Does the fork() method always asynchronously call the compute() method in every implementation of the fork() method? I am reading the API on the fork() method and the documentation does not mention the compute() method. Here is the URL to what I am typing about: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinTask.html#fork%28%29

Do you think there is a way to show that fork() calls compute() again someway with a System.out.println (SOP) statement?

Kind Regards,

T
Hello Campers!

Please let me start off by wishing my fellow Java-heads a very happy Towel Day! Now, to get straight to the biscuits...

I am reading the interesting and helpful book, "Oracle Certified Professional Java SE 7 Programmer Exams 1Z0-804 and 1Z0-805 - A Comprehensive OCPJP7 Certification Guide" by Ganesh and Sharma (2013). In this book there is an interesting program that illustrates how to use some classes from the java.util.concurrent.*; package. One I am trying to understand with my limited bonobos brain has to do with the "fork/join framework". I modified the code located in the Ganesh and Sharma (2013) book, referenced above, so that I could understand what the program was doing.

My question is: What is the fork() method doing in the program? Is it recursively calling the compute() method or is it summing up the numbers without making sure the value is not too large in the first if statement in the compute() method. That if statement is: if ( (to - from) <= N/NUM_THREADS)

The full program is below. I made some formatting modifications to display number values using the US locale and also to print some more debugging information such as count and note every time compute() is called. I also count every time fork() is called and then print out the totals upon program completion to verify that in fact every time fork() is called, compute() is again called from itself as a recursive method invocation.


The Code:




Thank-you for reading this post, especially on Hitchhiker's Guide to the Galaxy Day!

Respectfully,

Tedster

Jeanne Boyarsky wrote:

Ted North wrote:Do the two dots mean to move back one directory to the previous directory, which is just above the 'programs' directory? So since the path is going into the 'programs' directory then directly out of it with the double periods the normalize() method removes it?


Exactly.

Ted North wrote:For instance in a BASH shell typing: cd ../ will move back one directory. Where as cd <directory-name> moves to the typed directory.

This is even stranger because I rarely use Windows any more and the question is with a Windows file system.


Stick with that mental model. cd .. works on Windows too. Really, it does. Java tries to be platform independent so you can switch the directory path separators. Which means I could write c:/OCPJP7 and Java will be happy as a clam.



Cool. Thank-you Jeanne for helping me to understand this normalize() method. I am glad Java is platform independent. I can write once and run it everywhere.

P.S. - That was interesting to read about cd on Windows. Working with the operating-system (OS) in a shell is often times more fun than a GUI like Explorer on Windows or Nautilus on Ubuntu.

Sincerely,

Ted
Jeanne,

Thank-you for the helpful reply. I think I understand that the single dot is removed - just because it does not make a lot of sense in a path to a file but the two dots are confusing still. 0_o It is sort of surprising that the Path compiles at all with the strange dots in the middle of the URL or URI.

Do the two dots mean to move back one directory to the previous directory, which is just above the 'programs' directory? So since the path is going into the 'programs' directory then directly out of it with the double periods the normalize() method removes it?

For instance in a BASH shell typing: cd ../ will move back one directory. Where as cd <directory-name> moves to the typed directory.

This is even stranger because I rarely use Windows any more and the question is with a Windows file system.

I hope this made sense to someone other than me.

Sincerely,

Ted
Hey all,

I am still reading the G&S book thanks to enough free time, working computer, and relatively good health. Some people think C and Python are the best languages but I am continuing to venture down this binary Java programming road of mystery and wonder. Without further ado, could someone please explain to me how the normalize() method from the Path class is working in this example found on page 279 of the G&S book on passing the OCP Java 7 SE Programmer 2 exam?

Here is teh codez:



Thank-you for reading Java Ranch dot com. I hope everyone is having an awesome time and living out their wildest dreams everyday.

Sincerely,

Ted
Thanks Java Ranch for helping me understand regular expressions more! I read everyone's responses and it has helped me understand in much greater detail what is happening in the IP regular expression.

I am understanding how the individual components function together as a whole now. This may not be an incredibly difficult programming syntax to decipher after all.

I see how if there is a \\d meta-symbol after the brackets this symbolizes that there should be any digit (0-9) unless said meta-symbol is followed by a question mark which means that this is optional or can appear at the most a single time. Plus the OR symbols are not as confusing as before. I do not think I understood at first that only one of these options is being chosen at a time.

Thank-you again Java Ranch. I am sure I will have more questions for the board soon.

Sincerely,

Ted

Rico Felix wrote:

Ted North wrote:What is the operator that determines if three numbers appear, two numbers, or just one since an IP could be 192.168.300.300 or 192.168.1.1 etc?



[01]?\\d\\d? is used to get one digit or two digits or three digits since two digits are optional...



Rico,

Thank-you for explaining this confusing regular expression stuff.

So with the ORs | - the expression can choose only one of these? So in this case it would either be a number starting with 25 or 2 or 0 or 1?

Also, when would the regex engine choose the zero in [01] towards the end of the regex? I have never seen an IP such as 192.168.1.01

Sincerely,

Ted

Rico Felix wrote:

Henry Wong wrote:For large regexes, it is generally a good idea to break it down to it's components.



That is exactly what you must do to make sense of long regex's...

We start with "\\b((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)(\\.)){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\b" and move left to right just like the regex engine would:

Seeing that you have a fair amount of knowledge on regex's from your last post (the image), skipping over the obvious parts we decipher through the non-obvious

25[0-5] translates to 250 min to 255 max

2[0-4]\\d translates to 200 min to 249 max

[01]?\\d\\d? translates to 0 min to 199 max (remember ? mean 0 or one)



Thank you for the explanation of the numbers Rico. This really helped. I see how the \\d's represent any number from zero through nine now.

What is the operator that determines if three numbers appear, two numbers, or just one since an IP could be 192.168.300.300 or 192.168.1.1 etc?

Respectfully,

Ted