Commit 06a6194f authored by Elzbieta Pustulka's avatar Elzbieta Pustulka
Browse files

rest of the thread package from Parsons

parent 0f2abd37
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Sensor class - demonstrates a thread that waits for synchronized code
*/
public class Analyzer extends Thread
{
private Sensor dataSource;
public Analyzer(Sensor sensor)
{
dataSource = sensor;
dataSource.start();
}
@Override
public void run()
{
while (true)
{
double[] data = dataSource.getResults();
double result = 0.0;
for (int i = 0; i < data.length; i++)
{
result += data[i];
}
System.out.println("Result: " + result);
}
}
public static void main(String[] args)
{
Analyzer sensorDataAnalyzer = new Analyzer(new Sensor());
sensorDataAnalyzer.start();
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Bear class - a subclass of Thread
*/
public class Bear extends Thread
{
@Override
public void run()
{
// the bear sleeps at first
try
{
Thread.sleep(50);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
// when it wakes up, it goes straight for the honey. because it
// has a higher priority than the bees, they have to stop until
// the bear has finished
System.out.println("Mmmmmm, honey, yum yum!");
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Demonstrates giving different threads different priorities
*/
public class BearBeesAndHoney
{
public static void main(String[] args)
{
// the bees have minimum priority
Bees honeyBees = new Bees();
honeyBees.setPriority(Thread.MIN_PRIORITY);
// the bear has maximum priority
Bear hungryBear = new Bear();
hungryBear.setPriority(Thread.MAX_PRIORITY);
honeyBees.start();
hungryBear.start();
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Demonstrates the effect of a yielding thread
*/
public class BearYieldingBeesAndHoney
{
public static void main(String[] args)
{
YieldingBees honeyBees = new YieldingBees();
Bear hungryBear = new Bear();
honeyBees.start();
hungryBear.start();
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Bees class - a subclass of Thread
*/
public class Bees extends Thread
{
// the bees buzz around their honey all the time, but can be
// interrupted by the bear who has a higher priority
@Override
public void run()
{
for (int i = 0; i < 20000; i++)
{
System.out.print("z");
if(i%100 == 0)
{
System.out.println();
}
}
}
}
package threads;
/**
* Code exercise from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Exercise 16.2
*/
public class Exercise16_2
{
public static void main(String args[])
{
RaceHorse lightning = new RaceHorse("Lightning");
RaceHorse thunder = new RaceHorse("Thunder");
RaceHorse pegasus = new RaceHorse("Pegasus");
lightning.setPriority(Thread.MIN_PRIORITY);
thunder.setPriority(Thread.MIN_PRIORITY);
pegasus.setPriority(Thread.MAX_PRIORITY);
lightning.start();
thunder.start();
pegasus.start();
}
}
\ No newline at end of file
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Hare class - a subclass of Thread
*/
public class Hare extends Thread
{
public Hare(String name)
{
super(name);
}
// the 'run' method displays the journey of the hare
@Override
public void run()
{
System.out.println(getName() + " has started racing");
// a short time to get to the oak tree..
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(getName() + " has passed the oak tree");
// a short time to get to the top of the hill
try
{
Thread.sleep(1000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(getName()
+ " is at the top of the hill (and has fallen asleep)");
// falls asleep for a long time
try
{
Thread.sleep(20000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
// gets to the end (after the tortoise, probably)
System.out.println(getName() + " has finished!");
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* RacingTortoise class - a subclass of Thread
*/
public class RacingTortoise extends Thread
{
public RacingTortoise(String name)
{
super(name);
}
// 'run' displays the journey of the tortoise
@Override
public void run()
{
// slow but steady progress to the oak tree
System.out.println(getName() + " has started racing");
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(getName() + " has passed the oak tree");
// slow but steady progress to the top of the hill
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(getName() + " is at the top of the hill");
// slow but steady progress to the checkered flag
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
System.out.println(getName() + " has finished!");
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Sensor class - demonstrates synchronized code
*/
public class Sensor extends Thread
{
public static final int MAX_READINGS = 10;
private volatile double[] results = new double[MAX_READINGS];
private volatile int index = 0;
private boolean ready = false;
public synchronized double[] getResults()
{
if (!ready)
{
try
{
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
return results;
}
public synchronized void addResult(double result)
{
System.out.println("sensor reading " + result);
results[index] = result;
if (index == MAX_READINGS - 1)
{
ready = true;
notify();
ready = false;
index = 0;
}
else
{
index++;
}
}
@Override
public void run()
{
while (true)
{
try
{
Thread.sleep(200);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
addResult(Math.round(Math.random() * 10));
}
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Demonstrates running multiple threads
*/
public class TheTortoiseAndTheHare
{
public static void main(String[] args)
{
// create two separate thread objects (a tortoise and a hare)
Hare racingHare = new Hare("Hare");
RacingTortoise racingTortoise = new RacingTortoise("Tortoise");
// start them both racing
racingHare.start();
racingTortoise.start();
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* Tortoise class - a Thread subclass
*/
public class Tortoise extends Thread
{
// set the name of the thread in the constructor
public Tortoise(String name)
{
super(name);
}
// override the 'run' method to provide the behavior of the thread
@Override
public void run()
{
// local variable to store a random waiting time
int sleepTime;
// loop ten times (the tortoise travels ten meters)
for (int i = 0; i < 10; i++)
{
// use 'getName' to display the name of the thread
System.out.println(getName() + " has gone " + i + " metres");
// generate a random delay
sleepTime = (int) (Math.random() * 1000);
// 'sleep' for the specified time
try
{
Thread.sleep(sleepTime);
}
catch (InterruptedException e)
{
System.out.println("Interrupted");
}
}
System.out.println(getName() + " has finished!");
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* TortoiseRunner class - starts a Tortoise thread running
*/
public class TortoiseRunner
{
public static void main(String[] args)
{
// create a 'Tortoise' object
Tortoise racingTortoise = new Tortoise("Tortoise");
// use its 'start' method to make its thread run
racingTortoise.start();
}
}
package threads;
/**
* Code example from "Foundational Java: Key Elements and Practical Programming" 2nd Edition, Springer, 2020
*
* @author David Parsons
*
* YieldingBees class - demonstrates a yielding thread
*/
public class YieldingBees extends Thread
{
@Override
public void run()
{
for (int i = 0; i < 20000; i++)
{
System.out.print("z");
if(i%100 == 0)
{
System.out.println();
Thread.yield();
}
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment