Newbie: Thread myThread = Thread.currentThread(); ????

Z

Zalek Bloom

What is a meaning of the line:
Thread myThread = Thread.currentThread();

I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();

I took it from the code below:
http://java.sun.com/docs/books/tutorial/essential/threads/clock.html

import java.awt.Graphics;
import java.util.*;
import java.text.DateFormat;
import java.applet.Applet;

public class Clock extends Applet implements Runnable {
private Thread clockThread = null;
public void start() {
if (clockThread == null) {
clockThread = new Thread(this, "Clock");
clockThread.start();
}
}
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {
repaint();
try {
Thread.sleep(1000);
} catch (InterruptedException e){
// the VM doesn't want us to sleep anymore,
// so get back to work
}
}
}
public void paint(Graphics g) {
// get the time and convert it to a date
Calendar cal = Calendar.getInstance();
Date date = cal.getTime();
// format it and display it
DateFormat dateFormatter = DateFormat.getTimeInstance();
g.drawString(dateFormatter.format(date), 5, 10);
}
// overrides Applet's stop method, not Thread's
public void stop() {
clockThread = null;
}
}

Thanks,

Zalek
 
X

xarax

Zalek Bloom said:
What is a meaning of the line:
Thread myThread = Thread.currentThread();

I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();
/snip/

Actually, you don't understand "myThread.currentThread()",
because it means *exactly* the same thing as "Thread.currentThread()".

currentThread() is a static method, not an instance method. That's
why it is called with the class name, rather than an instance.

It returns the Thread object that represents the current
thread.
 
B

Bjorn Abelli

"Zalek Bloom" ...

[Please don't crosspost.
Followup set to only comp.lang.java.help]
What is a meaning of the line:
Thread myThread = Thread.currentThread();

It returns the Thread-instance currently executing.
I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();

What you would accomplish with that is to create a *new* Thread, and in the
next sentence just abandon it, since the variable will get the reference to
another Thread-instance.
public void run() {
Thread myThread = Thread.currentThread();
while (clockThread == myThread) {

It's a construction to make sure that it really is the designated Thread
you're working with, in case you have started other Threads as well.


// Bjorn A
 
M

Michael Borgwardt

Zalek said:
What is a meaning of the line:
Thread myThread = Thread.currentThread();

It's a static method, meaning that it is not specific to an object and
can be called via the class name.

I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();

That would have the same effect, but invoking static methods through
an object reference is considered very bad style.
 
Z

Zalek Bloom

What is a meaning of the line:
Thread myThread = Thread.currentThread();

I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();

I took it from the code below:
http://java.sun.com/docs/books/tutorial/essential/threads/clock.html
[..]

OK - let me explain more what I don't understand:
From class java.lang.Thread I quote:
"static Thread currentThread() -
Returns a reference to the currently executing thread object".

Thread (from "Thread.currentThread();" statement) in not a Thread
OBJECT - it is a CLASS.
Now, there may be more than one "currently executing thread object".
So which reference returns method currentThread()?

Thanks,

Zalek
 
T

Tim Ward

Zalek Bloom said:
OK - let me explain more what I don't understand:
From class java.lang.Thread I quote:
"static Thread currentThread() -
Returns a reference to the currently executing thread object".

Now, there may be more than one "currently executing thread object".
So which reference returns method currentThread()?

Well, there may indeed be more than one currently executing thread object if
you have more than one CPU, but I think you'll find what currentThread()
returns is the thread that made the call to currentThread() regardless of
whether any other threads are currently also running on other CPUs.
 
B

Bjorn Abelli

...
From class java.lang.Thread I quote:
"static Thread currentThread() -
Returns a reference to the currently executing thread object".

Thread (from "Thread.currentThread();" statement)
in not a Thread OBJECT - it is a CLASS.
Now, there may be more than one "currently executing
thread object".
So which reference returns method currentThread()?

I think it's easier for you to look at it this way:

Since static methods not are bound to any particular instance of the class,
the class itself can be referenced to as a singular object containing only
the static methods and attributes. Hence, you can call static methods by
sending the messages directly to the class.

(a bit simplified, I know, but...)

It's *that* "reference" that returns the "current thread".

This particular method is just checking in which thread the call is made,
and returns it.

// Bjorn A
 
J

John C. Bollinger

Zalek said:
What is a meaning of the line:
Thread myThread = Thread.currentThread();

I would understand if it was written:
Thread myThread = new Thread() ;
myThread = myThread.currentThread();

I took it from the code below:
http://java.sun.com/docs/books/tutorial/essential/threads/clock.html

[..]

OK - let me explain more what I don't understand:
From class java.lang.Thread I quote:
"static Thread currentThread() -
Returns a reference to the currently executing thread object".

Thread (from "Thread.currentThread();" statement) in not a Thread
OBJECT - it is a CLASS.

There is no need to shout. Those who are trying to help you know very
well that Thread is a class and that its currentThread() method is
static. At least two of the three previous respondants already
addressed the issue explicitly.
Now, there may be more than one "currently executing thread object".
So which reference returns method currentThread()?

Short answer: Thread.currentThread() returns a reference to the Thread
object that represents the thread that invoked the method.

It is crucial here to understand static methods. Static methods are
associated with classes, not with specific objects. Static methods can
(and should) be invoked via the associated class name. Static methods
can also (but should not) be invoked via an instance of the class -- or
at least it can appear that way in source code. In fact, invoking a
static method through a reference-type expression does not use the
_value_ of the expression at all, only its type. That can be confusing
on multiple levels, which is precisely why you shouldn't do it.

It is also crucial here to understand the difference between a thread of
execution and a Thread object. A thread is a logically consecutive
sequence of operations performed inside the VM; a Thread is best
regarded as a "handle" or "control panel" for a thread. Every operation
performed by a program occurs in the context of exactly one thread. The
operation can even be considered at some level to be performed _by_ the
thread.

That explains what the Thread docs are trying to tell you: any
invocation of Thread.currentThread() occurs in the context of
(alternatively: is performed by) exactly one thread; the method returns
a reference to the Thread representing that thread. Which thread that
is has very little to do with the details of the expression in which
that method invocation is found. Furthermore, the Thread returned might
or might not be a Thread created by the program explicitly -- it might,
for instance, be a Thread representing the main thread, which is created
by the JVM. It might also be a Thread created in library code somewhere.

Programming with threads involves some difficult concepts that we have
so far only brushed. I strongly recommend that you develop a sound
understanding of the underlying language before complicating things by
diving into multithreading.


John Bollinger
(e-mail address removed)
 
M

Mark Haase

Michael Borgwardt said:
It's a static method, meaning that it is not specific to an object and
can be called via the class name.



That would have the same effect, but invoking static methods through
an object reference is considered very bad style.

The question I have is why do they bother with this at all? Why use the
thread object itself as a flag?

Are they trying to prevent other objects from calling the Clock's run
method? Or making sure that run() returns when the clock Thread is
disposed? I thought that in the latter case it would be much more
obvious just to use a boolean, for instance "running".
 
Z

Zalek Bloom

From Sun Java trail on:
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

"a subclass cannot override methods that are declared static in the
superclass. In other words, a subclass cannot override a class
method.".

To test this I wrote this code:

package test9;
import com.sun.java.swing.UIManager;
class Mammal
{
static String aMethod()
{
return "Original Mammal method ";
}
}

public class Human extends Mammal
{
static String aMethod()
{
return "overridden Human method ";
}
public static void main(String argv[])
{
Human h = new Human();
System.out.println("Human aMethod() = " + h.aMethod());

Mammal b = new Mammal();
System.out.println("Mammal aMethod() = " + b.aMethod());
}
}

In this code a static method aMethod from class Mammals is overriden
in subclass Human.
This code was compiled without any errors and got results:

AppAccelerator(tm) 1.1.034 for Java (JDK 1.1), x86 version.
Copyright (c) 1998 Borland International. All Rights Reserved.
Human aMethod() = overridden Human method
Mammal aMethod() = Original Mammal method

Any explanations?

Thanks,

Zalek
 
N

nobody

Zalek said:
From Sun Java trail on:
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

"a subclass cannot override methods that are declared static in the
superclass. In other words, a subclass cannot override a class
method.".

To test this I wrote this code:

package test9;
import com.sun.java.swing.UIManager;
class Mammal
{
static String aMethod()
{
return "Original Mammal method ";
}
}

public class Human extends Mammal
{
static String aMethod()
{
return "overridden Human method ";
}
public static void main(String argv[])
{
Human h = new Human();
System.out.println("Human aMethod() = " + h.aMethod());

Mammal b = new Mammal();
System.out.println("Mammal aMethod() = " + b.aMethod());
}
}

In this code a static method aMethod from class Mammals is overriden
in subclass Human.
This code was compiled without any errors and got results:

AppAccelerator(tm) 1.1.034 for Java (JDK 1.1), x86 version.
Copyright (c) 1998 Borland International. All Rights Reserved.
Human aMethod() = overridden Human method
Mammal aMethod() = Original Mammal method

Any explanations?

Thanks,

Zalek

It doesn't override the method; there are two distinct class methods,
Mammal.aMethod and Human.aMethod. To demonstrate, try changing the main
to this:

public static void main(String argv[])
{
Mammal h = new Human(); // declare it as a mammal
System.out.println("Human aMethod() = " + h.aMethod());

Mammal b = new Mammal();
System.out.println("Mammal aMethod() = " + b.aMethod());
}

You will see that it statically binds aMethod to the Mammal.aMethod,
rather than dynamically binding as is done with instance methods.
 
S

Shane Mingins

Try this then:

Mammal c = new Human();
System.out.println("Human or Mammal aMethod() = " + c.aMethod());

If you had infact /overridden/ anything you would be expecting the method on
Human to be called. And it is not.

Shane
--
(e-mail address removed)

remove clothes before replying

"It is not the strongest of the species that survive, nor the most
intelligent, but the one most responsive to change." --- Charles Darwin
 
J

John C. Bollinger

Zalek said:
From Sun Java trail on:
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

"a subclass cannot override methods that are declared static in the
superclass. In other words, a subclass cannot override a class
method.".

To test this I wrote this code:

package test9;
import com.sun.java.swing.UIManager;
class Mammal
{
static String aMethod()
{
return "Original Mammal method ";
}
}

public class Human extends Mammal
{
static String aMethod()
{
return "overridden Human method ";
}
public static void main(String argv[])
{
Human h = new Human();
System.out.println("Human aMethod() = " + h.aMethod());

Mammal b = new Mammal();
System.out.println("Mammal aMethod() = " + b.aMethod());
}
}

In this code a static method aMethod from class Mammals is overriden
in subclass Human.

I'm afraid you are mistaken.
This code was compiled without any errors and got results:

AppAccelerator(tm) 1.1.034 for Java (JDK 1.1), x86 version.
Copyright (c) 1998 Borland International. All Rights Reserved.
Human aMethod() = overridden Human method
Mammal aMethod() = Original Mammal method

Any explanations?

You don't understand what the tutorial was telling you. To put it
another way, static methods are not polymorphic. That makes some sense
when you remember that they are associated with classes, not objects,
but would make even more sense if Java did not allow them to be invoked
through reference-type expressions.

Try changing your main method to this:
public static void main(String argv[])
{
Mammal h = new Human();
Mammal b = new Mammal();
Mammal n = null;
Human h2 = h;
Human n2 = null;

System.out.println("aMethod() invoked on class Mammal = "
+ Mammal.aMethod());
System.out.println("aMethod() invoked \"on\" a Mammal = "
+ b.aMethod());
System.out.println("aMethod() invoked \"on\" a Human Mammal = "
+ h.aMethod());
System.out.println("aMethod() invoked \"on\" a null Mammal = "
+ n.aMethod());
System.out.println("aMethod() invoked on class Human = "
+ Human.aMethod());
System.out.println("aMethod() invoked \"on\" a Human = "
+ h2.aMethod());
System.out.println("aMethod() invoked \"on\" a null Human = "
+ n2.aMethod());
}
}

You should see that the result depends only on the _type_ of the
reference variable via which the static method is invoked, not on its
value (which can even be null).


The other thing hidden in the tutorial's words is that a subclass cannot
declare an _instance_ method that conflicts with a superclass' static
method. So, for instance, if you take the "static" modifier off
Human.aMethod(), the resulting source will cause a compilation error.


John Bollinger
(e-mail address removed)
 
Z

Zalek Bloom

Shane Mingins said:
Try this then:

Mammal c = new Human();
System.out.println("Human or Mammal aMethod() = " + c.aMethod());

If you had infact /overridden/ anything you would be expecting the method on
Human to be called. And it is not.

Shane

It looks I didn't understand what overriding is.
So how can I override a Mammal aMethod()?

Thanks,

Zalek

Zalek Bloom said:
From Sun Java trail on:
http://java.sun.com/docs/books/tutorial/java/javaOO/override.html

"a subclass cannot override methods that are declared static in the
superclass. In other words, a subclass cannot override a class
method.".

To test this I wrote this code:

package test9;
import com.sun.java.swing.UIManager;
class Mammal
{
static String aMethod()
{
return "Original Mammal method ";
}
}

public class Human extends Mammal
{
static String aMethod()
{
return "overridden Human method ";
}
public static void main(String argv[])
{
Human h = new Human();
System.out.println("Human aMethod() = " + h.aMethod());

Mammal b = new Mammal();
System.out.println("Mammal aMethod() = " + b.aMethod());
}
}

In this code a static method aMethod from class Mammals is overriden
in subclass Human.
This code was compiled without any errors and got results:

AppAccelerator(tm) 1.1.034 for Java (JDK 1.1), x86 version.
Copyright (c) 1998 Borland International. All Rights Reserved.
Human aMethod() = overridden Human method
Mammal aMethod() = Original Mammal method

Any explanations?

Thanks,

Zalek
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top