ThreadGroup group = new ThreadGroup("MyThreadGroup");
MainStation mainStation = new MainStation();
Thread stationThread= new Thread(group,mainStation);
TrainA trainA = new TrainA(mainStation);
TrainB trainB=new TrainB(mainStation);
Thread trainAThread=new Thread(group,trainA);
Thread trainBThread=new Thread(group,trainB);
stationThread.setDaemon(true);
ExecutorService executorService= Executors.newFixedThreadPool(3);
executorService.submit(stationThread);
executorService.submit(trainAThread);
executorService.submit(trainBThread);
executorService.shutdown();
< /code>
Aber wenn ich das Programm ausführe, führt nur ein Zug -Thread mit dem Bahnhofsthread aus und nicht mit beiden Zug -Threads. < /p>
Main stattion
Train a running
Train B running
TrainA Removing people from station value--0
TrainB Removing people from station value--0
Station adding people to queue value--0 size--1
TrainA Queue size after removing --0
TrainA Removing people from station value--1
Station adding people to queue value--1 size--0
TrainA Queue size after removing --0
Station adding people to queue value--2 size--1
TrainA Removing people from station value--2
TrainA Queue size after removing --0
Station adding people to queue value--3 size--1
TrainA Removing people from station value--3
TrainA Queue size after removing --0
Station adding people to queue value--4 size--1
TrainA Removing people from station value--4
TrainA Queue size after removing --0
Station adding people to queue value--5 size--1
TrainA Removing people from station value--5
TrainA Queue size after removing --0
Station adding people to queue value--6 size--1
TrainA Removing people from station value--6
TrainA Queue size after removing --0
Station adding people to queue value--7 size--1
TrainA Removing people from station value--7
TrainA Queue size after removing --0
Station adding people to queue value--8 size--1
TrainA Removing people from station value--8
TrainA Queue size after removing --0
Station adding people to queue value--9 size--1
TrainA Removing people from station value--9
TrainA Queue size after removing --0
< /code>
Wenn Sie beobachten, dass der obige Protokoll nur Menschen vom Bahnhof entfernt und nicht den Zug b.public class MainStation implements Runnable{
private volatile ConcurrentLinkedDeque queue=new ConcurrentLinkedDeque();
private final static People people=new People(10);
@Override
public void run() {
synchronized(this){
for (int i = 0; i < people.getCount(); i++) {
//System.out.println("Station adding people to queue "+i);
this.queue.add(i);
System.out.println("Station adding people to queue value--" + i + " size--" + this.queue.size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
public ConcurrentLinkedDeque getQueue() {
return queue;
}
< /code>
} < /p>
Züge A und B < /p>
public class TrainA implements Runnable{
private MainStation station=null;
public TrainA(MainStation station){
this.station=station;
}
@Override
public void run() {
System.out.println("Train a running");
while (true) {
for (int i = 0; i < station.getQueue().size(); i++) {
System.out.println("TrainA Removing people from station value--" + station.getQueue().getFirst());
if(station!=null&&station.getQueue()!=null)station.getQueue().removeFirst();
System.out.println("TrainA Queue size after removing --" + station.getQueue().size());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}
Wie im folgenden Code -Snippet gezeigt, gibt es einen Bahnhof und zwei Züge. Station ist ein Faden, und Züge sind zwei verschiedene Fäden.[code] ThreadGroup group = new ThreadGroup("MyThreadGroup"); MainStation mainStation = new MainStation(); Thread stationThread= new Thread(group,mainStation); TrainA trainA = new TrainA(mainStation); TrainB trainB=new TrainB(mainStation); Thread trainAThread=new Thread(group,trainA); Thread trainBThread=new Thread(group,trainB);
stationThread.setDaemon(true);
ExecutorService executorService= Executors.newFixedThreadPool(3); executorService.submit(stationThread); executorService.submit(trainAThread); executorService.submit(trainBThread); executorService.shutdown(); < /code> Aber wenn ich das Programm ausführe, führt nur ein Zug -Thread mit dem Bahnhofsthread aus und nicht mit beiden Zug -Threads. < /p> Main stattion Train a running Train B running TrainA Removing people from station value--0 TrainB Removing people from station value--0 Station adding people to queue value--0 size--1 TrainA Queue size after removing --0 TrainA Removing people from station value--1 Station adding people to queue value--1 size--0 TrainA Queue size after removing --0 Station adding people to queue value--2 size--1 TrainA Removing people from station value--2 TrainA Queue size after removing --0 Station adding people to queue value--3 size--1 TrainA Removing people from station value--3 TrainA Queue size after removing --0 Station adding people to queue value--4 size--1 TrainA Removing people from station value--4 TrainA Queue size after removing --0 Station adding people to queue value--5 size--1 TrainA Removing people from station value--5 TrainA Queue size after removing --0 Station adding people to queue value--6 size--1 TrainA Removing people from station value--6 TrainA Queue size after removing --0 Station adding people to queue value--7 size--1 TrainA Removing people from station value--7 TrainA Queue size after removing --0 Station adding people to queue value--8 size--1 TrainA Removing people from station value--8 TrainA Queue size after removing --0 Station adding people to queue value--9 size--1 TrainA Removing people from station value--9 TrainA Queue size after removing --0 < /code> Wenn Sie beobachten, dass der obige Protokoll nur Menschen vom Bahnhof entfernt und nicht den Zug b.public class MainStation implements Runnable{
private volatile ConcurrentLinkedDeque queue=new ConcurrentLinkedDeque(); private final static People people=new People(10);
@Override public void run() { synchronized(this){ for (int i = 0; i < people.getCount(); i++) { //System.out.println("Station adding people to queue "+i); this.queue.add(i); System.out.println("Station adding people to queue value--" + i + " size--" + this.queue.size()); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
}
public ConcurrentLinkedDeque getQueue() { return queue; } < /code> } < /p> Züge A und B < /p> public class TrainA implements Runnable{
private MainStation station=null;
public TrainA(MainStation station){ this.station=station; }
@Override public void run() { System.out.println("Train a running"); while (true) { for (int i = 0; i < station.getQueue().size(); i++) { System.out.println("TrainA Removing people from station value--" + station.getQueue().getFirst()); if(station!=null&&station.getQueue()!=null)station.getQueue().removeFirst(); System.out.println("TrainA Queue size after removing --" + station.getQueue().size()); try { Thread.sleep(1000); } catch (InterruptedException e) { throw new RuntimeException(e); } } }
Ich arbeite an einem Python-Skript, das prüft, ob eine gehashte Dokumentnummer mit einer Dokumentnummer in einer großen Liste von Datensätzen übereinstimmt, die in einer MySQL-Datenbank gespeichert...
Ich habe eine Fish-Klasse erstellt, die einen Fisch simuliert, der sich in einem Java-Programm reproduziert. Jeder Fisch muss sich fortpflanzen, indem er einen anderen zufälligen Fisch trifft. Das...
Ich habe mehrere YouTube-Demos und Tutorials zur Implementierung von Multithread-Operationen in Java verfolgt. Alle Tutorials zeigen jedoch das folgende Verfahren:
class Task implements Runnable {...
Ich arbeite an einer Java-Anwendung, bei der ich einen großen Datensatz aus aus der Datenbank abgerufenen Zeilen verarbeiten muss. Hier ist die Beispielsituation:
class Example {
@Autowired
private...