Finished assigment

main
2wenty1ne 2024-10-14 18:00:36 +02:00
commit 54e159b6d5
5 changed files with 71 additions and 0 deletions

View File

@ -0,0 +1,11 @@
class MyWorker implements Runnable {
private Thread self;
@Override
public void run() {
this.self = Thread.currentThread();
while (true) {
System.out.println(this.self.getName() + ": ID => " + this.self.threadId());
}
}
}

View File

@ -0,0 +1,12 @@
class MyWorkerCoop implements Runnable {
private Thread self;
@Override
public void run() {
this.self = Thread.currentThread();
while (true) {
System.out.println(this.self.getName() + ": ID => " + this.self.threadId());
Thread.yield();
}
}
}

View File

@ -0,0 +1,11 @@
public class Starter {
static int WORKERS = 200;
public static void main(String... args) {
for (var i = 0; i < Starter.WORKERS; i++) {
// var t = new Thread(new MyWorker(), String.format("Worker-%03d", i));
var t = new Thread(new MyWorkerCoop(), String.format("Worker-%03d", i));
t.start();
}
}
}

View File

@ -0,0 +1,20 @@
public class StarterInner {
static int WORKERS = 200;
public static void main(String[] args) {
for (var i = 0; i < Starter.WORKERS; i++) {
var t = new Thread(new Runnable() {
private Thread self;
@Override
public void run() {
this.self = Thread.currentThread();
while(true) {
System.out.println(this.self.getName() + ": ID => " + this.self.threadId());
}
}
}, String.format("Worker-%03d", i));
t.start();
}
}
}

View File

@ -0,0 +1,17 @@
public class StarterLambda {
static int WORKERS = 200;
public static void main(String... args) {
for (var i = 0; i < Starter.WORKERS; i++) {
var t = new Thread(
() -> {
var self = Thread.currentThread();
while(true) {
System.out.println(self.getName() + ": ID => " + self.threadId());
}
},String.format("Worker-%03d", i));
t.start();
}
}
}