1 / 13

Unit 1 Lab16

Unit 1 Lab16. Understanding Threads. Remember, when creating a Thread, you must use an object that implements the Runnable interface Thread Constructor header in Thread class: public Thread(Runnable arg) Using the thread constructor in Lab14 class:

brade
Download Presentation

Unit 1 Lab16

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 1 Lab16

  2. Understanding Threads • Remember, when creating a Thread, you must use an object that implementsthe Runnable interface • Thread Constructor header in Thread class: • public Thread(Runnable arg) • Using the thread constructor in Lab14 class: • Swimmer karel = new Swimmer(2); //Swimmer class implements Runnable • Thread t1 = new Thread(karel);

  3. Understanding Threads • Because Thread was defined knowing that the class you use with it contains the run() method, the start() method from the Thread class invokes the run method…

  4. public class Lab14 { public static void main(String[] args) {Swimmer weismuller = new Swimmer(2);Swimmer fraser = new Swimmer(4);Swimmer spitz = new Swimmer(6);Swimmer phelps = new Swimmer(8);Thread t1 = new Thread( weismuller );Thread t2 = new Thread( fraser );Thread t3 = new Thread( spitz );Thread t4 = new Thread( phelps );t1.start();t2.start();t3.start();t4.start(); } } public class Thread { public Thread(Runnable arg) { /*Constructor definition not shown*/ } public void start() { run(); } } public class Swimmer implements Runnable { //code not shown public void run() { //code not shown } }

  5. Implementing Interfaces • You can extend only one class • You can implement numerous interfaces public class Shifter extends Robot implementsRunnable, Workable

  6. Visibility Modifiers • public -can be accessed/used from anywhere (inside of any class) • private-can only be accessed from inside the class in which it was created • protected – default visibility modifier (when none is specified). Can be accessed by any class in the same package or by any subclass.

  7. Visibility Modifiers contd • Constructors and methods that you want to be able to use in other classes are public. • Example: Inside of the Athlete Class, the constructors and methods (turnRight() & turnAround() ) are public. Inside of Lab02, you can use these constructors and methods. If they were private, you could not. • Attributes or data fields are usually declared private so that they cannot be directly changed from outside of the class.

  8. Data Fields • Recall that some resource classes have attributes (data fields) and behaviors (methods) • What data fields are in the Robot class? Direction, Location, and # of Beepers

  9. Creating Data Fields • Also called instance data - each object made gets its own copy of the data field. • They are declared inside of a class, above the constructors and methods. • They are usually defined with the private visibility modifier • They are usually initialized inside of the constructor.

  10. Creating Data Fields public class Shifter extends Robot implements Runnable, Workable { private int myBeepers; //data field declared public Shifter(int y) { //Call to super must be first command super(1, y, Display.EAST, 0); //data field initialized myBeepers = 0; }

  11. Changing Data Fields public class Shifter extends Robot implements Runnable, Workable { NOTE: This is not the real code for workCorner!!! … public void workCorner() { int x = 0; //not a data field while(nextToBeeper()) { pickBeeper(); myBeepers++; //unique for each object x++; } } Saves value Does not

  12. Starting Lab16 • Lab16 is already complete • Open Shifter shell • Since Shifter implements Workable and Runnable, what methods do you have to define? • public void moveOneBlock(); - just like Harvester/Carpenter • public void turnToTheRight(); - just like Harvester/Carpenter • public void turnToTheNorth(); - just like Harvester/Carpenter • public void workCorner(); • public void run();

  13. Starting Lab16 • public void workCorner(); • Create an integer and set it to zero. • Pick up all the beepers in the pile you are standing on (you do not know how many), increase the integer you just created by one each time you pick up a beeper. • Put down all the beepers from the previous pile. You know exactly how many to put down, that number is represented by the variable myBeepers • Make myBeepers become equal to the integer you created so that it will save the value of how many beepers you just picked up. • public void run(); • How can each Shifter complete the entire task?

More Related