Doubts...

Discussion in 'Java' started by getsanjay.sharma@gmail.com, Apr 15, 2007.

  1. Guest

    Hello to all programmers out there.

    I have some concepts which were very fuzzy for me so I just wanted
    them to get clarified.

    · What is the difference between the import statement and the
    Class.forName() when all they do is load a class?

    · What is the difference between the run() and start() method of a
    thread? Why do we need to call start() when we can directly call run()
    esp when start() is something like:

    protected void start()
    {
    if(target != null)
    target.run();
    }

    Thanks in advance.
     
    , Apr 15, 2007
    #1
    1. Advertising

  2. | <> wrote in message
    | news:...
    | Hello to all programmers out there.
    |
    | I have some concepts which were very fuzzy for me so I just wanted
    | them to get clarified.
    |
    | · What is the difference between the import statement and the
    | Class.forName() when all they do is load a class?

    Import informs the compiler of the full package names of classes so that you
    don't have to type the whole java.lang.String every time you want a String.
    It has no effect at runtime because the generated code includes the full
    names. Class.forName() takes only the full class name and makes it
    available via reflection. It's used specifically when your program cannot
    know the name of the class at compile time and so it is meaningful only at
    runtime.

    | · What is the difference between the run() and start() method of a
    | thread? Why do we need to call start() when we can directly call run()
    | esp when start() is something like:

    The run method of a thread contains the thread's application functionality.
    Naturally, it's empty--your application fills in the run portion either by
    subclassing Thread (not recommended) or by supplying a Runnable (preferred).
    The run method contents is what will be executed in a separate thread. The
    start method activates that functionality in a separate stream of execution.
    It's what causes the seemingly parallel execution to occur.

    |
    | protected void start()
    | {
    | if(target != null)
    | target.run();
    | }

    You have to remember that the above code is executing in a separate stream
    of execution. If you call run directly, it will execute sequentially with
    the caller. Only start () will activate the separate thread. Consider:

    Runnable r = new Runnable () {
    public void run () {
    System.out.println ("Running.");
    }};

    Thread t = new Thread (r);

    PROGRAM #1
    t.run ()
    System.out.println ("Done.");

    PROGRAM #2
    t.start ()
    System.out.println ("Done.");

    --------
    The first program always prints Running and then Done--there is no
    parallelism.
    The second program can print the two strings either order because they're
    happening in separate, parallel threads of execution. Start fires off the
    thread and its "run" will occur elsewhere a short time later. Maybe before
    the "Done" occurs, maybe after. It may be difficult to demonstrate this
    effect because of OS timing peculiarities--most books on threading have a
    much better example that shows two interfering threads. "Concurrent
    Programming in Java" by Doug Lea is a worthwhile read.

    Cheers,
     
    Matt Humphrey, Apr 15, 2007
    #2
    1. Advertising

  3. Kai Schwebke Guest

    Why do you post same question two times?

    You may consider choosing a more meaningful subject for your posts.



    Kai
     
    Kai Schwebke, Apr 15, 2007
    #3
  4. <> wrote in message
    news:...
    Hello to all programmers out there.

    After answering your question I found you had asked the same thing within
    the past hour in a separate message. Please don't waste people's time in
    the future by not reading the answers you get and expect that an answer may
    take more than an hour.
     
    Matt Humphrey, Apr 15, 2007
    #4
  5. Lew Guest

    Matt Humphrey wrote:
    > <> wrote in message
    > news:...
    > Hello to all programmers out there.
    >
    > After answering your question I found you had asked the same thing within
    > the past hour in a separate message. Please don't waste people's time in
    > the future by not reading the answers you get and expect that an answer may
    > take more than an hour.


    Also, "doubts" means "suspicions", not "questions".

    --
    Lew
     
    Lew, Apr 15, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MS

    Help, .NET doubts.....

    MS, Jun 4, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    573
    Yuancai \(Charlie\) Ye
    Jun 4, 2004
  2. Replies:
    0
    Views:
    552
  3. Nisheeth

    xilinx ise doubts

    Nisheeth, Mar 30, 2005, in forum: VHDL
    Replies:
    0
    Views:
    589
    Nisheeth
    Mar 30, 2005
  4. Synchronizer doubts

    , Jul 20, 2005, in forum: VHDL
    Replies:
    2
    Views:
    1,733
  5. Synchronizer doubts

    , Jul 20, 2005, in forum: VHDL
    Replies:
    0
    Views:
    473
Loading...

Share This Page