diff options
| author | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
|---|---|---|
| committer | Sven Eisenhauer <sven@sven-eisenhauer.net> | 2023-11-10 15:11:48 +0100 |
| commit | 33613a85afc4b1481367fbe92a17ee59c240250b (patch) | |
| tree | 670b842326116b376b505ec2263878912fca97e2 /Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java | |
| download | Studium-master.tar.gz Studium-master.tar.bz2 | |
Diffstat (limited to 'Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java')
| -rw-r--r-- | Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java new file mode 100644 index 0000000..ce7d764 --- /dev/null +++ b/Master/Reference Architectures and Patterns/hjp5/examples/Listing2211.java @@ -0,0 +1,59 @@ +/* Listing2211.java */
+
+class Counter2211
+{
+ int cnt;
+
+ public Counter2211(int cnt)
+ {
+ this.cnt = cnt;
+ }
+
+ public int nextNumber()
+ {
+ int ret = cnt;
+ //Hier erfolgen ein paar zeitaufwändige Berechnungen, um
+ //so zu tun, als sei das Errechnen des Nachfolgezählers
+ //eine langwierige Operation, die leicht durch den
+ //Scheduler unterbrochen werden kann.
+ double x = 1.0, y, z;
+ for (int i= 0; i < 1000; ++i) {
+ x = Math.sin((x*i%35)*1.13);
+ y = Math.log(x+10.0);
+ z = Math.sqrt(x+y);
+ }
+ //Jetzt ist der Wert gefunden
+ cnt++;
+ return ret;
+ }
+}
+
+public class Listing2211
+extends Thread
+{
+ private String name;
+ private Counter2211 counter;
+
+ public Listing2211(String name, Counter2211 counter)
+ {
+ this.name = name;
+ this.counter = counter;
+ }
+
+ public static void main(String[] args)
+ {
+ Thread[] t = new Thread[5];
+ Counter2211 cnt = new Counter2211(10);
+ for (int i = 0; i < 5; ++i) {
+ t[i] = new Listing2211("Thread-"+i,cnt);
+ t[i].start();
+ }
+ }
+
+ public void run()
+ {
+ while (true) {
+ System.out.println(counter.nextNumber()+" for "+name);
+ }
+ }
+}
\ No newline at end of file |
