Unit 1 Lab 14 Lab 15 Thread Class
Unit 1 Lab 14 & Lab 15
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
Thread Class contd Used for synchronized running of code: concurrency. Since threads are built in, they don’t have to be imported.
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
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
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 } }
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 t 1 = new Thread(karel); /*start EACH thread with start method*/ t 1. start();
Starting Lab 14 Open Swimmer shell and edit… Implement the run method n 10 times in a row: w 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 Lab 14 (code is given to you in TJ packet) When you Finish 14, move on to 15
Starting Lab 15 Write Dancer class n Copy code directly from TJ Page One-44 Create 3 subclasses of Dancer n Use Back. And. Forth. Dancer as a model only w Your 3 subclasses should have different “dancer” names and dance. Step should be unique for each Write Lab 15 – using Lab 14 as a model n Should contain 1 of each of your new Dancers (3 Dancers in all)
- Slides: 9