You may work with a partner on this part; run it a number of times to get "weird" behavior. Answer the following questions:
// Make sure you add your name(s), command-line arguments, and answer the questions!
public class ThreadPlay
{
public static void main (String args[])
{
MyThread threadObj = new MyThread();
new Thread(threadObj,"Dog").start();
new Thread(threadObj,"Cat").start();
}
}
public class MyThread implements Runnable
{
private int _max = 20;
private int _count = 0;
private int _x;
private int _y;
private boolean yield = false;
public void setXandY(int x, int y)
{
_x = x;
//pause(100);
_y = y;
}
public void run()
{
String msg;
msg = Thread.currentThread().getName();
while (_count < _max)
{
setXandY(_count, _count + 1);
System.out.println(Thread.currentThread().getName()
+ " _count is " + _count + " x is " + _x
+ " y is " + _y);
_count++;
if (yield)
{
System.out.println(Thread.currentThread().getName() + " yielding");
Thread.yield();
}
}
msg = msg + " count: " + _count + " max: " + _max;
System.out.println(Thread.currentThread().getName() + " says bye-bye! " + msg);
}
void pause(int milliseconds)
{
try
{
Thread.sleep(milliseconds);
}
catch (InterruptedException e) {}
}
}