Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32874 > unrolled thread

The code being executed before hitting main() method

Started byJohn <xsli2@yahoo.com>
First post2015-10-14 06:26 -0700
Last post2015-10-14 19:08 +0200
Articles 9 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  The code being executed before hitting main() method John <xsli2@yahoo.com> - 2015-10-14 06:26 -0700
    Re: The code being executed before hitting main() method Eric Douglas <e.d.programmer@gmail.com> - 2015-10-14 06:51 -0700
      Re: The code being executed before hitting main() method Thomas Richter <thor@math.tu-berlin.de> - 2015-10-14 16:31 +0200
        Re: The code being executed before hitting main() method Eric Douglas <e.d.programmer@gmail.com> - 2015-10-14 08:54 -0700
          Re: The code being executed before hitting main() method Thomas Richter <thor@math.tu-berlin.de> - 2015-10-14 18:04 +0200
            Re: The code being executed before hitting main() method j4n bur53 <janburse@fastmail.fm> - 2015-10-14 18:49 +0200
    Re: The code being executed before hitting main() method John <xsli2@yahoo.com> - 2015-10-14 08:19 -0700
      Re: The code being executed before hitting main() method John <xsli2@yahoo.com> - 2015-10-14 10:04 -0700
      Re: The code being executed before hitting main() method j4n bur53 <janburse@fastmail.fm> - 2015-10-14 19:08 +0200

#32874 — The code being executed before hitting main() method

FromJohn <xsli2@yahoo.com>
Date2015-10-14 06:26 -0700
SubjectThe code being executed before hitting main() method
Message-ID<97d86c97-b889-44a1-931f-20b5af9c6ab5@googlegroups.com>
Hi:

I am investigating what code are being executed before flow hits main(). Here is my Java class:
public class Hello {
    public static void main(final String[] args) {
        final String str = "Hello World"; //set breakpoint here
        System.out.println(str);
    }
}

By setting the breakpoint, to my surprise, I saw a bunch code from Thread.class were executed before hand(and a bunch code were executed after printing statement, but let's focus on the code before hitting main()). I found this is very educational.

So I would guess that a thread was set up and my Hello program was inserted into that thread. But, by stepping through the execution, I saw many times the following code were executed. So it is not just one thread.

Any explanation will be highly appreciated. Thank you very much.


    /**
     * Allocates a new {@code Thread} object. This constructor has the same
     * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
     * {@code (group, null, name)}.
     *
     * @param  group
     *         the thread group. If {@code null} and there is a security
     *         manager, the group is determined by {@linkplain
     *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
     *         If there is not a security manager or {@code
     *         SecurityManager.getThreadGroup()} returns {@code null}, the group
     *         is set to the current thread's thread group.
     *
     * @param  name
     *         the name of the new thread
     *
     * @throws  SecurityException
     *          if the current thread cannot create a thread in the specified
     *          thread group
     */
    public Thread(ThreadGroup group, String name) {
        init(group, null, name, 0);
    } 

[toc] | [next] | [standalone]


#32875

FromEric Douglas <e.d.programmer@gmail.com>
Date2015-10-14 06:51 -0700
Message-ID<368b40af-0087-43df-9c55-2557e94897f1@googlegroups.com>
In reply to#32874
On Wednesday, October 14, 2015 at 9:27:30 AM UTC-4, John wrote:
> Hi:
> 
> I am investigating what code are being executed before flow hits main(). Here is my Java class:
> public class Hello {
>     public static void main(final String[] args) {
>         final String str = "Hello World"; //set breakpoint here
>         System.out.println(str);
>     }
> }
> 
> By setting the breakpoint, to my surprise, I saw a bunch code from Thread.class were executed before hand(and a bunch code were executed after printing statement, but let's focus on the code before hitting main()). I found this is very educational.
> 
> So I would guess that a thread was set up and my Hello program was inserted into that thread. But, by stepping through the execution, I saw many times the following code were executed. So it is not just one thread.
> 
> Any explanation will be highly appreciated. Thank you very much.
> 
> 
>     /**
>      * Allocates a new {@code Thread} object. This constructor has the same
>      * effect as {@linkplain #Thread(ThreadGroup,Runnable,String) Thread}
>      * {@code (group, null, name)}.
>      *
>      * @param  group
>      *         the thread group. If {@code null} and there is a security
>      *         manager, the group is determined by {@linkplain
>      *         SecurityManager#getThreadGroup SecurityManager.getThreadGroup()}.
>      *         If there is not a security manager or {@code
>      *         SecurityManager.getThreadGroup()} returns {@code null}, the group
>      *         is set to the current thread's thread group.
>      *
>      * @param  name
>      *         the name of the new thread
>      *
>      * @throws  SecurityException
>      *          if the current thread cannot create a thread in the specified
>      *          thread group
>      */
>     public Thread(ThreadGroup group, String name) {
>         init(group, null, name, 0);
>     }

The only code that executed before a main() is anything declared static in your main class.

[toc] | [prev] | [next] | [standalone]


#32876

FromThomas Richter <thor@math.tu-berlin.de>
Date2015-10-14 16:31 +0200
Message-ID<mvlovl$4jf$1@news2.informatik.uni-stuttgart.de>
In reply to#32875
Am 14.10.2015 um 15:51 schrieb Eric Douglas:
> 
> The only code that executed before a main() is anything declared static in your main class.
> 
Certainly not. The java runtime sets itself up, and that includes a lot
of the internal administration infrastructure, as for example the
garbadge collector.

Greetings,
	Thomas

[toc] | [prev] | [next] | [standalone]


#32878

FromEric Douglas <e.d.programmer@gmail.com>
Date2015-10-14 08:54 -0700
Message-ID<d48c956c-4746-4e92-8fba-c2f99248f62c@googlegroups.com>
In reply to#32876
On Wednesday, October 14, 2015 at 10:31:35 AM UTC-4, Thomas Richter wrote:
> Certainly not. The java runtime sets itself up, and that includes a lot
> of the internal administration infrastructure, as for example the
> garbadge collector.

Well of course there's internal code.  I wouldn't count that as code.  That would be like someone asking what they need to do before they can make their car go and instead of "turn the key and put it in gear" you're explaining how the spark plug has to get the pistons moving.

[toc] | [prev] | [next] | [standalone]


#32879

FromThomas Richter <thor@math.tu-berlin.de>
Date2015-10-14 18:04 +0200
Message-ID<mvluff$5vt$1@news2.informatik.uni-stuttgart.de>
In reply to#32878
Am 14.10.2015 um 17:54 schrieb Eric Douglas:
> On Wednesday, October 14, 2015 at 10:31:35 AM UTC-4, Thomas Richter wrote:
>> Certainly not. The java runtime sets itself up, and that includes a lot
>> of the internal administration infrastructure, as for example the
>> garbadge collector.
> 
> Well of course there's internal code.  I wouldn't count that as code.  

Why not? That's really a matter of perspective. So for example, in C
there is quite some startup code that uses the Os environment to prepare
the C startup.

In Java, it is even more involved as there is at first some native code
to startup the JVM, and then some java-internal startup data, written in
java itself, to initialize the environment.

The OP tries to understand this mechanism - this is as far as I
understand the post at least.

[toc] | [prev] | [next] | [standalone]


#32880

Fromj4n bur53 <janburse@fastmail.fm>
Date2015-10-14 18:49 +0200
Message-ID<mvm12k$70o$1@news.albasani.net>
In reply to#32879
Hi,

Thomas Richter schrieb:
> The OP tries to understand this mechanism - this is as far as I
> understand the post at least.

I guess there is no definite mechanism. It
depends on so many factors, from where the main()
method is ultimately called:
- Whether you use java
- Whether you use javaw
- Whether you double click a .jar which has a Main-Class attribute
- Whether you use IntelliJ
- Etc..

Bye

[toc] | [prev] | [next] | [standalone]


#32877

FromJohn <xsli2@yahoo.com>
Date2015-10-14 08:19 -0700
Message-ID<3f0fbfa8-66a0-4aa3-b9c3-b83dcc29d259@googlegroups.com>
In reply to#32874
This web page helps a lot:
https://wiki.csiro.au/pages/viewpage.action?pageId=545042739

[toc] | [prev] | [next] | [standalone]


#32881

FromJohn <xsli2@yahoo.com>
Date2015-10-14 10:04 -0700
Message-ID<0fec1882-4b73-468a-804e-6364703f8932@googlegroups.com>
In reply to#32877
This is my Java class:

public class Hello {
    public static void main(final String[] args){
        final String str = "Hello World";

        System.out.println(str);

        System.out.println("No. of active thread: " + Thread.activeCount());

        try {
            Thread.sleep(4000000); //let the 'java' process run for long time so i can run command 'jstack'
        }
        catch(final Exception e){
            System.out.println(e);
        }
    }
}
Run:
java Hello &
Then by running 'ps' command, I found the process id of 'java' -- which is 2554 on my hand, then run:
$jstack 2554
2015-10-14 11:27:55
Full thread dump Java HotSpot(TM) Server VM (24.71-b01 mixed mode):

"Attach Listener" daemon prio=10 tid=0x6c100800 nid=0xb05 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" daemon prio=10 tid=0x6c68a400 nid=0xa0b runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x6c688800 nid=0xa0a waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x6c686800 nid=0xa09 waiting on condition [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x6c684c00 nid=0xa07 runnable [0x00000000]
   java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=10 tid=0x6c674800 nid=0xa06 in Object.wait() [0x6fb7d000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x9efc4240> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
        - locked <0x9efc4240> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

"Reference Handler" daemon prio=10 tid=0x6c673000 nid=0xa05 in Object.wait() [0x6fd7d000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x9efc3ed8> (a java.lang.ref.Reference$Lock)
        at java.lang.Object.wait(Object.java:503)
        at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
        - locked <0x9efc3ed8> (a java.lang.ref.Reference$Lock)

"main" prio=10 tid=0xb7607400 nid=0x9fb waiting on condition [0xb7752000]
   java.lang.Thread.State: TIMED_WAITING (sleeping)
        at java.lang.Thread.sleep(Native Method)
        at temp.Hello.main(Hello.java:16)

"VM Thread" prio=10 tid=0x6c670800 nid=0xa04 runnable 

"GC task thread#0 (ParallelGC)" prio=10 tid=0xb7611400 nid=0x9fc runnable 

"GC task thread#1 (ParallelGC)" prio=10 tid=0xb7612800 nid=0x9fd runnable 

"GC task thread#2 (ParallelGC)" prio=10 tid=0xb7614000 nid=0x9fe runnable 

"GC task thread#3 (ParallelGC)" prio=10 tid=0xb7615800 nid=0x9ff runnable 

"GC task thread#4 (ParallelGC)" prio=10 tid=0xb7617000 nid=0xa00 runnable 

"GC task thread#5 (ParallelGC)" prio=10 tid=0xb7618400 nid=0xa01 runnable 

"GC task thread#6 (ParallelGC)" prio=10 tid=0xb7619c00 nid=0xa02 runnable 

"GC task thread#7 (ParallelGC)" prio=10 tid=0xb761b400 nid=0xa03 runnable 

"VM Periodic Task Thread" prio=10 tid=0x6c68c400 nid=0xa0c waiting on condition 

JNI global references: 106

Wow, so many threads! So many garbage collection threads for such a simple program! I am even wondering if the 'java' on this RedHat Linux needs to be configured.

[toc] | [prev] | [next] | [standalone]


#32882

Fromj4n bur53 <janburse@fastmail.fm>
Date2015-10-14 19:08 +0200
Message-ID<mvm260$9hl$1@news.albasani.net>
In reply to#32877
John schrieb:
> This web page helps a lot:
> https://wiki.csiro.au/pages/viewpage.action?pageId=545042739
>

The below article(*) somehow suggests that:
-XX:+UseSerialGC

And the following:
-XX:ParallelGCThreads=1 -XX:+UseParallelGC

Are the same.
Which seems not to be 100% true, I get:

java -XX:+PrintFlagsFinal -version | grep GC
uintx ParallelGCThreads                         = 4

java -XX:+UseSerialGC -XX:+PrintFlagsFinal -version | grep GC
uintx ParallelGCThreads                         = 0

(*)
http://blog.chrysocome.net/2013/05/what-happened-to-java-client.html


[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web