Doubts...

G

getsanjay.sharma

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.
 
M

Matt Humphrey

| | 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,
 
K

Kai Schwebke

Why do you post same question two times?

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



Kai
 
M

Matt Humphrey

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.
 
L

Lew

Matt said:
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".
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top