SLIDE 4 4
Review: Java Threads: The Basics
public public class class RunnableTask RunnableTask implements implements Runnable { Runnable { public public RunnableTask RunnableTask(…) { (…) { // save any arguments or input for the task (optional) } @Override public public void void run() { run() { // required to implement for Runnable interface … } } … RunnableTask task = new new RunnableTask RunnableTask(); (); Thread t1 = new new Thread( Thread(task task, , "thread1" "thread1"); ); t1.start();
Oct 17, 2018 Sprenkle - CSCI330 7
Java review: Tradeoffs of extending vs implementing
Example: Jabber
class class Jabber Jabber implements implements Runnable { Runnable { String str; public public Jabber(String Jabber(String s){ ){ str str = = s; } ; } public public void void run() { run() { while while (true true) { ) { System.out
.print(str str); ); System.out
.println(); (); } } } public public class class JabberTest JabberTest { public public static static void void main(String[] main(String[] args args) { ) { Jabber jabber1 = new new Jabber( Jabber("1" "1"); ); Jabber jabber2 = new new Jabber( Jabber("2" "2"); ); Thread t1 = new new Thread( Thread(jabber1); ); Thread t2 = new new Thread( Thread(jabber2); ); t1.start(); t2.start(); } }
Oct 17, 2018 Sprenkle - CSCI330 8
JabberTest.java What does this code do? What will the output be?