Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Doubts...

Reply
Thread Tools

Doubts...

 
 
getsanjay.sharma@gmail.com
Guest
Posts: n/a
 
      04-15-2007
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.

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      04-15-2007

| <(E-Mail Removed)> wrote in message
| news:(E-Mail Removed) ps.com...
| 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,







 
Reply With Quote
 
 
 
 
Kai Schwebke
Guest
Posts: n/a
 
      04-15-2007
Why do you post same question two times?

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



Kai
 
Reply With Quote
 
Matt Humphrey
Guest
Posts: n/a
 
      04-15-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
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.


 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      04-15-2007
Matt Humphrey wrote:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments