Ho una CPU a quattro core. Creo 4 thread ed eseguo un ciclo intensivo della cpu, e richiede> 4x più tempo rispetto a eseguirlo tutto proceduralmente in un thread.Threading Java di base (4 thread) più lento rispetto a non threading
Ho creato due progetti da confrontare, uno con threading e uno senza. Mostrerò il codice e i tempi di esecuzione. Basta notare il motivo per cui il progetto senza threading sembra strano è che volevo replicare il sovraccarico della memoria, perché non ero sicuro di quanto avrebbe effetto il tempo di esecuzione. Quindi, ecco il codice senza threading:
class TimeTest implements Runnable {
private Thread t;
private String name;
TimeTest(String name) {
this.name = name;
System.out.println("Creating class " + name);
}
public void run() {
System.out.println("Running class " + name);
int value = 100000000;
// try {
while (--value > 0) {
Math.random();
// Thread.sleep(1);
// System.out.println("Class " + name + " " + value);
}
// } catch (InterruptedException e) {
// System.out.println("Interrupted " + name);
// }
System.out.println("Class " + name + " exiting...");
}
public void start() {
System.out.println("Starting class " + name);
if (t == null) {
t = new Thread(this, name);
// t.start();
this.run();
}
}
}
public class ThreadComp {
public static void main(String[] args) {
TimeTest one = new TimeTest("Class-1");
one.start();
TimeTest two = new TimeTest("Class-2");
two.start();
TimeTest three = new TimeTest("Class-3");
three.start();
TimeTest four = new TimeTest("Class-4");
four.start();
}
}
Questo funziona in circa 11 secondi.
Ecco il codice con filettatura:
class RunnableTest implements Runnable {
private Thread t;
private String name;
RunnableTest(String name) {
this.name = name;
System.out.println("Creating thread " + name);
}
public void run() {
System.out.println("Running thread " + name);
int value = 100000000;
// try {
while (--value > 0) {
Math.random();
// Thread.sleep(1);
// System.out.println("Thread " + name + " " + value);
}
// } catch (InterruptedException e) {
// System.out.println("Interrupted " + name);
// }
System.out.println("Thread " + name + " exiting...");
}
public void start() {
System.out.println("Starting thread " + name);
if (t == null) {
t = new Thread(this, name);
t.start();
}
}
}
public class ThreadTest {
public static void main(String[] args) {
RunnableTest one = new RunnableTest("Thread-1");
one.start();
RunnableTest two = new RunnableTest("Thread-2");
two.start();
RunnableTest three = new RunnableTest("Thread-3");
three.start();
RunnableTest four = new RunnableTest("Thread-4");
four.start();
}
}
Questo viene eseguito in circa 1 minuto 13 secondi.
Ora, nell'esempio da cui sto imparando, chiamano Thread.sleep durante il lead per 50ms. Se faccio questo, i thread girano più velocemente SE chiamo anche Thread.sleep (50) sulla classe non-threaded.
Che è fantastico, so come farlo funzionare. Ma la ragione per cui sto imparando questo è che sto facendo pathfinding, e non aggiungerò una chiamata Sleep su qualcosa che richiede già molto tempo e non ha bisogno di mettere in pausa e non fare nulla nemmeno per 1ms (a meno che non assolutamente deve).
Quindi, quello che mi chiedo è che cosa mi manca? I thread devono assolutamente essere messi a dormire o l'oggetto deve attendere affinché funzionino come intendo (cioè eseguendo tutti e quattro i loop in parallelo)?
Anche se sto facendo un errore, perché ci vorrà molto più tempo? Penserei allo scenario peggiore, funzionerebbe ancora in 11 secondi, finirebbe semplicemente in un ordine imprevedibile ...
così - ci vorranno 4 donne 36 mesi per produrre un bambino? – ZhongYu
Posso quasi garantire che l'uso di 'Math.random' è la causa di questo problema. Vedi https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random-- –
^^ Cosa dice @JAtkin. Il tuo codice è essenzialmente tutto conteso. –