AFCCG thread

J

jupiter

Help, there's a bug in my thread. The stud keeps winning.



public static void main(String[] args) {

AFCCG championship = new AFCCG();

Thread stud = new Thread(championship);

Thread goober = new Thread(championship);//"Pass that Runnable;
pass that Runnable"


stud.setName("Tom");

goober.setName("Peyton");


stud.setPriority(Thread.MAX_PRIORITY);

goober.setPriority(Thread.MIN_PRIORITY);


stud.start();

goober.start();

}



synchronized void run() {

int studCount = 0;

int gooberCount = 0;

for(int i = 1; i <10; i++) {

throwTDPass();

if(currentThread.getName().equals("Tom")) { studCount++}
else {gooberCount--}

}

if(studCount > gooberCount) {

System.out.println("Cut that meat; cut that meat!");

}

}
 
P

Phi

I think, your code does not have to do much with Priorites, nor with
multithreading.

Some more output lines show, that stud always win over goober; no way,
which has the higher priority.

Here some more print statements in the same code:

public class AFCCG implements Runnable{
public static void main(String[] args) {
AFCCG championship = new AFCCG();
Thread stud = new Thread(championship);
Thread goober = new Thread(championship);//"Pass that Runnable;
stud.setName("Stud");
goober.setName("Goober");
stud.setPriority(Thread.MIN_PRIORITY);
goober.setPriority(Thread.MAX_PRIORITY);
stud.start();
goober.start();
}
public void run() {
int studCount = 0;
int gooberCount = 0;
for(int i = 1; i <10; i++) {
throwTDPass();
Thread currentThread = Thread.currentThread();
if(currentThread .getName().equals("Stud"))
{ System.out.println("Stud");studCount++;}
else
{System.out.println("Goober");gooberCount--;}
}
if(studCount > gooberCount) {
System.out.println("I am " + Thread.currentThread().getName() +
" std: " + studCount + ", goob: " + gooberCount);
}
}
private void throwTDPass() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Opened your eyes?
 
J

jupiter

Phi said:
I think, your code does not have to do much with Priorites, nor
with
multithreading.

}
if(studCount > gooberCount) {
System.out.println("I am " +
Thread.currentThread().getName() +
" std: " + studCount + ", goob: " + gooberCount);
}
}
private void throwTDPass() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}


Opened your eyes?

Sigh. It was you that changed fate.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top