zu blöd für switch case?

Status
Nicht offen für weitere Antworten.

Giftstachel

Bekanntes Mitglied
moinmoin, liebe mitleidenden.

irgendwie hab ich nen brett vor dem kopf, oder es geht wirklich nicht.
ich möchte in einem switch-case mehrere threads aufrufen.

Code:
switch(seitenZahl) {
				case 1:{this.run1(1);System.out.println("1");break;}
				case 2:{this.run1(2);System.out.println("2");break;}
				case 3:{this.run1(3);System.out.println("3");break;}
				case 4:{this.run1(4);System.out.println("4");break;}
				case 5:{this.run1(5);System.out.println("5");break;}
				case 6:{this.run1(3);this.run2(3);System.out.println("6");break;}
				case 7:{this.run1(4);this.run2(3);System.out.println("7");break;}
				case 8:{this.run1(4);this.run2(4);System.out.println("8");break;}
				case 9:{this.run1(5);this.run2(4);System.out.println("9");break;}
				case 10:{this.run1(5);this.run2(5);System.out.println("10");break;}
				case 11:{this.run1(6);this.run2(5);System.out.println("11");break;}
				case 12:{this.run1(6);this.run2(6);System.out.println("12");break;}
				case 13:{this.run1(5);this.run2(4);this.run3(4);System.out.println("13");break;}
				case 14:{this.run1(5);this.run2(5);this.run3(4);System.out.println("14");break;}
				case 15:{this.run1(5);this.run2(5);this.run3(5);System.out.println("15");break;}
				case 16:{this.run1(6);this.run2(5);this.run3(5);System.out.println("16");break;}
				case 17:{this.run1(6);this.run2(6);this.run3(5);System.out.println("17");break;}
				case 18:{this.run1(6);this.run2(6);this.run3(6);System.out.println("18");break;}
				default : {System.out.println("hamma mist baut?");break;}
			}

nur irgendwie geht er mit über den ersten thread nicht raus, startet also bei 6 nur this.run1()

warum, was hab ich vergessen?

danke euch 🙂
 
Blind geraten: deine run1-Methode startet keinn Thread (start-Methode) sondern ruft nur run auf. In der steht ne Endlosschleife, deshalb kommts nie zu der nächsten run.
 
hmmm. ok. grübel...

im effendi sieht das ganze komplett so aus:

Code:
public class StarteThreads extends Thread{
		int readcounter = 0;
		public void blablabla(int seitenZahl){
			System.out.println("narf");
			switch(seitenZahl) {
				case 1:{this.run1(1);System.out.println("1");break;}
				case 2:{this.run1(2);System.out.println("2");break;}
				case 3:{this.run1(3);System.out.println("3");break;}
				case 4:{this.run1(4);System.out.println("4");break;}
				case 5:{this.run1(5);System.out.println("5");break;}
				case 6:{this.run1(3);this.run2(3);System.out.println("6");break;}
				case 7:{this.run1(4);this.run2(3);System.out.println("7");break;}
				case 8:{this.run1(4);this.run2(4);System.out.println("8");break;}
				case 9:{this.run1(5);this.run2(4);System.out.println("9");break;}
				case 10:{this.run1(5);this.run2(5);System.out.println("10");break;}
				case 11:{this.run1(6);this.run2(5);System.out.println("11");break;}
				case 12:{this.run1(6);this.run2(6);System.out.println("12");break;}
				case 13:{this.run1(5);this.run2(4);this.run3(4);System.out.println("13");break;}
				case 14:{this.run1(5);this.run2(5);this.run3(4);System.out.println("14");break;}
				case 15:{this.run1(5);this.run2(5);this.run3(5);System.out.println("15");break;}
				case 16:{this.run1(6);this.run2(5);this.run3(5);System.out.println("16");break;}
				case 17:{this.run1(6);this.run2(6);this.run3(5);System.out.println("17");break;}
				case 18:{this.run1(6);this.run2(6);this.run3(6);System.out.println("18");break;}
				case 19:{this.run1(5);this.run2(5);this.run3(5);this.run4(4);System.out.println("19");break;}
				case 20:{this.run1(5);this.run2(5);this.run3(5);this.run4(5);System.out.println("20");break;}
				case 21:{this.run1(6);this.run2(5);this.run3(5);this.run4(5);System.out.println("21");break;}
				case 22:{this.run1(6);this.run2(6);this.run3(5);this.run4(5);System.out.println("22");break;}
				case 23:{this.run1(6);this.run2(6);this.run3(6);this.run4(5);System.out.println("23");break;}
				case 24:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);System.out.println("24");break;}
				case 25:{this.run1(5);this.run2(5);this.run3(5);this.run4(5);this.run5(5);System.out.println("25");break;}
				case 26:{this.run1(6);this.run2(5);this.run3(5);this.run4(5);this.run5(5);System.out.println("26");break;}
				case 27:{this.run1(6);this.run2(6);this.run3(5);this.run4(5);this.run5(5);System.out.println("27");break;}
				case 28:{this.run1(6);this.run2(6);this.run3(6);this.run4(5);this.run5(5);System.out.println("28");break;}
				case 29:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(5);System.out.println("29");break;}
				case 30:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(6);System.out.println("30");break;}
				case 31:{this.run1(6);this.run2(5);this.run3(5);this.run4(5);this.run5(5);this.run6(5);System.out.println("31");break;}
				case 32:{this.run1(6);this.run2(6);this.run3(5);this.run4(5);this.run5(5);this.run6(5);System.out.println("32");break;}
				case 33:{this.run1(6);this.run2(6);this.run3(6);this.run4(5);this.run5(5);this.run6(5);System.out.println("33");break;}
				case 34:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(5);this.run6(5);System.out.println("34");break;}
				case 35:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(6);this.run6(5);System.out.println("35");break;}
				case 36:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(6);this.run6(6);System.out.println("36");break;}
				case 37:{this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(6);this.run6(7);System.out.println("37");break;}
				default : {System.out.println("hamma mist baut?");break;}
			}
			System.out.println("narf nach switch");
			
		}
		public void run1(int seitenZahl){	
			System.out.println(" run 1 seitenzahl = " + seitenZahl);
			
//******mach ne ganze menge genau 10k mal *****************
				
			for(int count = 0; count < 10000; count++) {
				
						try{
							Thread.sleep(1000);
						}
						catch (InterruptedException e) {
			                        e.getMessage();
			                        }
						
				}
			}
		}
                public void run2(int seitenZahl){
               // mach was
                }
                public void run3(int seitenZahl){
               // mach was
                }
                public void run4(int seitenZahl){
               // mach was
                }
                public void run5(int seitenZahl){
               // mach was
                }
                public void run6(int seitenZahl){
               // mach was
                }
                public void run7(int seitenZahl){
               // mach was
                }


}

da ist zwar keine endlosschleife im ursprünglichen sinne drin, aber so was ähnliches, da er bei jedem fehler absichtlich den thread neu startet... öhm... das muss er aber auch....

nächste (vermutlich doch irgendwie sinnvolle) frage:
in einen array? ich habe noch nie gehört, das man threads in einem aaray starten kann, aber das klingt iwie interessant. das sollte gehen wie?

danke euch 🙂
 
wie gesagt: nur weil du eine beliebig eigensbenannte Operation aufrufst,
wird Java nicht überlegen 'ach, der User will hier bestimmt einen Thread, dann mach ich mal einen auf'

so funktioniert programmieren nicht, Threads brauchen bestimmte Regeln,
normale Operationsaufrufe werden dagegen ganz normal hintereinander ausgeführt,
run2() erst nach dem Ende von run1()

----------

zum Array kann ich grad nix sagen, scheint mir zunächst mal nix besonders zu verkürzen, was nicht auch auf anderen Weg ginge,

z.B.
statt
this.run1(6);this.run2(6);this.run3(6);this.run4(6);this.run5(6);
kürzer
this.runAlle(6,6,6,6,6);

und in dieser Hilfsoperation wird run1 bis x mit dem richtigen Parameter aufgerufen
 
oh. klar. was für ein geistiger geniestreich meinerseits... logisch, ich muss ihm ja auch sagen, das er als thread laufen soll^^

manchmal hilft eben doch nur ein wink mit dem gartenzaun.

danke dir 🙂
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben