wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Brogden's Exam Cram and Thread and static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Brogden Watch "Brogden New topic
Author

Brogden's Exam Cram and Thread and static

Dale Lobach
Greenhorn

Joined: Apr 18, 2001
Posts: 4
I'm finally biting the bullet and taking the exam on Monday. While reviewing Brogden's JEC I'm stumped on one of the examples on "static". The code will follow. The problem is that when the static variable is always coming up with the same value even when I have more than one thread running. Perrhaps I don't understand what the example is to show. Here are the (slightly modified) class files:
The CountDown Class:
====================
import java.awt.TextField;
import java.awt.event.*;
public class CountDown extends java.lang.Object implements java.lang.Runnable
{
static int counters = 0;
static synchronized int incCount()
{
return ++counters;
}
TextField text;
int myN;
CountDown( TextField tf2 )
{
text = tf2;
Thread t = new Thread( this );
System.out.println( "Start counter " + myN );
t.start();
myN = incCount();
}
public void run()
{
for( int i = 10; i >= 0; i-- )
{
try{
text.setText( Integer.toString( i ) );
Thread.sleep( 1000 );
}catch( InterruptedException e ) {}
}
}
}

The FramenThread Class (which calls CountDown):
===============================================
import java.awt.*;
import java.awt.event.*;
import CountDown.*;
class FramenThread extends Frame
{
public static void main( String args[] )
{
FramenThread fnt = new FramenThread();
fnt.show();
CountDown cd = new CountDown( fnt.tf );
try{ Thread.sleep( 6000 ); }catch ( Exception e ){}
CountDown cd2 = new CountDown( fnt.tf2 );
}
FramenThread()
{
setBounds( 10, 10, 200, 150 );
setLayout( new FlowLayout() );
add( tf );
add( tf2 );
addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
setVisible( false );
dispose();
System.exit( 0 );
}
} );
}
TextField tf = new TextField( "0" );
TextField tf2 = new TextField( "9" );
}
-----------------
The problem is... the variable "counters" always returns a 0 and as a result the variable myN is also always zero. I thought that being static in this situation it would increment when a second instance is run.
Where am I missing it?
Thanks,
Dale Lobach
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Hi Dale,
Per your code, the constructor of each CountDown Threat will print out the default value of int myN, which is 0 before the Threat start(). I believe counters actually advances. To test the theory just set int myN = counters; and retry the code again.
Regards,
Lam

[This message has been edited by Lam Thai (edited April 18, 2001).]
Stevie Kaligis
Ranch Hand

Joined: Feb 04, 2001
Posts: 400
Hi Dale...
static int counters = 0 //belongs to the class
int myN; //belongs to each instance of the class, default = 0
let see the CountDown Constructor :
CountDown(....) {
....
....
System.out.println("Start Counter " + myN); //all CountDown object will get value of zero
....
myN = incCount(); assign return value from incCount() method
}
try remove "System.out.println("Start Counter" + myN);" to the last line of constructor, and add this code :
System.out.println("static int counters : " + counters);
I would suggest:
don't use myN for print the counters, use counters variable, it is more elegant.
hope that helps
stevie
Dale Lobach
Greenhorn

Joined: Apr 18, 2001
Posts: 4
I did what Stevie suggested and printed the counter from the constructor. Yep, they increment. I guess I'm not sure what Mr. B was trying to do in his book with myN.
Thanks guys.
Dale Lobach
Lam Thai
Ranch Hand

Joined: Apr 02, 2001
Posts: 117
Originally posted by Dale Lobach:
I did what Stevie suggested and printed the counter from the constructor. Yep, they increment. I guess I'm not sure what Mr. B was trying to do in his book with myN.
Thanks guys.
Dale Lobach

Hi Dale,
Let's give it a thought to why the variable 'counters' must be assigned to myN.
Could it be the reason that the variable 'counters' should not be directly accessed because it is 'static', a class variable (i.e the only one shared by all objects)?
I mean, if all instances share the same variable, could there be a situation when more than one threads try to access it?
What prevent the threads from doing so unless we somehow synchorinize the access?
Maybe that is the reason for the existence of the synchronized method named incCount(), isn't it?
Else life without synchronization would be much simpler, class variable can be chaotically accessed.... Darwinism comes into play... May the fastest and the strongest win (ouch)!
I might read too much into B's mind but just a bit of food for thought.
Regards,
Lam

[This message has been edited by Lam Thai (edited April 19, 2001).]
 
Consider Paul's rocket mass heater.
 
subject: Brogden's Exam Cram and Thread and static
 
Similar Threads
Countdown timer in java
getting null pointer exception
How to make JFrame/JPanel update BEFORE idle time
Threads and ActionListeners
Exception in thread