• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

child threads calling methods in master class

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two classes that extend Applet implement Runnable:
1. class Elevator
2. class Passenger
If a Passenger wants to ride the Elevator, it must
use a method in the Elevator class called:
pushElevatorButton()
I am confused on how and where to declare my Elevator and
Passengers. Since there is only one Elevator and there
are multiple Passengers, it makes sense that the Elevator
is going to be the master class. So, how do I make sure
that my Passenger threads are calling the method(s) defined
in the Elevator class that created them?
Thank You,
Jeff
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,
Sounds like the Passenger objects need to keep a reference to their parent Elevator. Since Elevators create Passengers, you could pass the reference in the Passenger c'tor.
jply
 
Jeff, Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jerry,
Thanks for the advice, but I am not completely
convinced the problem is that my Passenger class
is not accessing the Elevator's methods/variables
like I said in my original post. So, instead
I hobbled my code and came up with this shortened
version that should simply create the Elevator and
run it and then create the Passenger and run it.
I think that my Passenger is not even running at
all because nothing is being printed in the
second text box during run time. I know it is a
simple mistake that I am making, but I am new to
Java and not sure about all of its syntax rules.
Here is the code:
// Master Thread = Elevator class
import java.awt.*;
import java.applet.Applet;
public class Elevator extends Applet implements
Runnable
{
static TextField text1, text2;
public void init()
{
text1 = new TextField(20);
add(text1);
text2 = new TextField(20);
add(text2);
// Create Threads for Elevator and Passenger
Thread Elevator = new Thread(this);
Elevator.start();
Passenger Mel = new Passenger();
Mel.start();
}// end of init

// The Elevator's run method
public void run()
{
while(true)
{
try
{
Thread.sleep(50);
} catch(InterruptedException e) {}
text1.setText("Elevator Running");
}// end of while
}// end of run()
}// end of Elevator class

import java.awt.*;
import java.applet.Applet;
public class Passenger extends Applet implements
Runnable
{
Elevator myElevator;
// constructor will take a reference
// from calling object after I am sure
// the Passenger thread is running at all
public void Passenger()
{
} // end of constructor
// The Passenger's run method
public void run()
{
while(true)
{
try
{
Thread.sleep(50);
} catch(InterruptedException e) {}
Elevator.text2.setText("Passenger Running");
}// end of while
}// end of run()
}// end of Passenger class
Thank You,

Jeff Jones
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,
Just a small basic mistake. You forgot to create a thread for Passenger !!
Check this code in Elevator class -
Passenger Mel = new Passenger();
Mel.start();
A little modification reqd -
Passenger Mel = new Passenger();
Thread Passenger = new Thread(Mel);
Passenger.start();
I'm a newcomer to the world of Java.
I was surprized to see that a thread object named Elevator is allowed inside the class with same name. Does that not become confusing? On the contrary, is there any specific reason/advantage in doing so?
------------------
Regards,
Pravin.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here are a few tips that will help you clean up your code and figure this out.
First of all, don't ever give variables the same name as a class (i.e., naming your thread "Elevator"). It's just unneccessary confusion.
Here's the rule that Sun suggests: start all class names with an Uppercase letter, and all variables with a lower-case letter. So.. name your thread "elevatorThread" instead (notice how that describes what the thing actually is, too).

Second, you only need to extend Applet with classes that are "Applets" -- i.e., they're going to be a panel sitting in a web browser window. Try having Passenger not extend anything (technically, Java will set it up automatically to extend Object). Then you'll notice that Mel.start() only compiled because Applet has a start method (even though it makes no sense for a Passenger)... follow Pravin's advice on how to fix that error, and good luck!
-Rob
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic