Download presentation
Presentation is loading. Please wait.
1
Unit 1 Lab14 & Lab15
2
Thread Class Built in JAVA class – exists in the “real world” of JAVA
Allows us to create processes running on the computer separate from the main program’s execution Parallel processing: different robots performing their own methods seemingly at the same time
3
Thread Class contd Used for synchronized running of code: concurrency.
Since threads are built in, they don’t have to be imported.
4
Thread Constructor public Thread(Runnable arg) What does this mean?
When you make a Thread object, you have to put in a a Runnable object as a parameter. However, Runnable is actually an interface so there are no direct Runnable objects, therefore you must put in an object of a class that implements Runnable
5
Swimmer Class public class Swimmer extends Robot implements Runnable If Swimmer implements the Runnable interface, what does this mean? Swimmer must define all abstract methods in the Runnable interface
6
public interface Runnable { public abstract void run(); }
public class Swimmer implements Runnable { public Swimmer(int x) { super(x, 1, Display.NORTH, 0); } public void run() //You define } }
7
Using Threads with Swimmers
//create Swimmer object Swimmer karel = new Swimmer(2); /*create a Thread object FOR EACH SWIMMER, using a Swimmer object for the actual parameter*/ Thread t1 = new Thread(karel); /*start EACH thread with start method*/ t1.start();
8
Starting Lab14 Open Swimmer shell and edit… Implement the run method
10 times in a row: Move forward twice, twirl all the way around, then move back to the starting position and turn back around to prepare for the next iteration. Write Lab14 (code is given to you in TJ packet) When you Finish 14, move on to 15
9
Starting Lab15 Write Dancer class Create 3 subclasses of Dancer
Copy code directly from TJ Page One-44 Create 3 subclasses of Dancer Use BackAndForthDancer as a model only Your 3 subclasses should have different “dancer” names and danceStep should be unique for each Write Lab15 – using Lab14 as a model Should contain 1 of each of your new Dancers (3 Dancers in all)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.