Demonstrating Threads
I want to write a simple program which will demonstrate the power of threads. My idea is to write two methods: one which counts up from 1 to 10000 and the other counts down from 10000 to 1. Respectively named countUp() and countDown().
I want to set countUp() going in a thread and then set countDown(). The idea is to demonstrate that the two methods run simultaneously even though they are coded one after the other:
this.countUp();
this.countDown();
The problem I have is this: I want to display the progress of each method to the cmd line like below:
UPDOWN
-
110000
29999
39998
.....
100001
How can I make two different methods running inside two separate threads System.out.println() to the same cmd window in the way above.
Thanks in advance
[833 byte] By [
Haclanda] at [2007-10-2 6:04:03]

If the threads were that predictable, you could have one doing print() and the other doing println().
But if that were the case, it would kind of defeat the purpose of threads.
You have no knowledge of or control over when a thread gets scheduled. For something as quick as counting to 10,000, with no blocked I/O, one thread will probably complete its task before the scheduler switches to the other one.
To get them to go in lock step, you'd have to use synhcronization and wait/notify, but then that degenerates to an effectively sequential operation.
You putSystem.out.println(something)in their code. What's your problem with that?
> You putSystem.out.println(something)
in
> their code. What's your problem with that?
He wants T1's and T2's values on the same line.
One thing I didn't think of that you could do is to make the counters member variables and then call print from outside of T1 and T2: System.out.println(obj1.counter + ", " + obj2.counter);
Then of course, you need syncing or volatile to make sure the printing thread sees the counting threads' values.
And of course you'd have to make sure the printing thread got cycles while the counting threads are doing their thing, so that you could see the values increasing.
I know how to Sys.out.println() !!I guess I want to launch the program from one cmd line and have it spawn another to allow one of the threads to print() to. Is this possible?
What's this "command line" you're talking about?
A JVM has only one System.in and only one System.out. Those are what are usually called the "command line", although if you're running your code in an IDE they may be modelled by a window of some kind.
So if you want more than one "command line" then you have to start another JVM. And you certainly aren't going to get the data to line up in a pretty way as targaryen interprets your question. This post is getting pretty incoherent.
you're right - it is getting incoherent, my apologies.I think I might have found the answer: if i wrote it as an applet then I could have both threads paint to the screen simultaneously. Thanks anyway, sometimes all it takes to solve a problem is to share it ....H
hmm. you could write a third thread (hence,demonstrating threads ;) ) which would take values after some time:
thread1
in run:
while (i>0)
{
i--;
sleep(300);
}
thread2
in run:
while (i <10000)
{
i++;
sleep(300); //or any random value!
}
thread3
in run:
int c1 = thread1.getValue();// you need to define a getter in those threads!
int c2 = thread2.getValue();
while ( c1 > 0 || c2 < 10000)
{
//maybe insert some checks here
System.println( "thread1: " + c1 + "\t thread2" + c2);
sleep(100);
}
or something like that. you could cache the values in the 3rd thread and only print them when they've changed.
you can't rely that the threads will do things simultaneously and it would be no good demonstration if they did.
metaa at 2007-7-16 13:04:28 >

It's not much interesting as a threading example because such output could be gotten from a
plain single-threaded program. The reason for postint it here is that I want to share my experience
in trying to use java.util.concurrent synchronizers, semaphore family of objects. From my
experience, I think those semaphore-like objects are desighned for controlling threads, but not
the parts of them. Typical 'parts' of a thread are iterations with loops. I struggled to use those
synchronizers for controlling each iteration, and finally gave up. In the code below, only the
starter cue has remained as a trace of my long and futile struggle.
(**** in the code is a variable name "go" + "OK", which is censored by the forum software.)
import java.util.concurrent.*;
public class UpAndDown{
public static int upCounter, downCounter;
public static boolean upOK, dpOK, goOK;
public static CountDownLatch startCue;
public static void main(String[] args){
upCounter = 0;
downCounter = 1001;
upOK = dpOK = goOK = false;
UpAndDown uad = new UpAndDown();
startCue = new CountDownLatch(1);
uad.go();
}
void go(){
new Up().start();
new Down().start();
startCue.countDown();
while (true){
if (upOK && dpOK){
upOK= dpOK = goOK = false;
System.out.println(upCounter + " " + downCounter);
goOK = true;
}
if (downCounter < 1){
break;
}
}
}
class Up extends Thread{
public void run(){
try{
startCue.await();
}
catch (Exception ee){
ee.printStackTrace();
}
goOK = true;
while (upCounter < 1001){
if (goOK){
++upCounter;
upOK = true;
}
try{
Thread.sleep(100);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
class Down extends Thread{
public void run(){
try{
startCue.await();
}
catch (Exception ee){
ee.printStackTrace();
}
goOK = true;
while (downCounter > 0){
if (goOK){
--downCounter;
dpOK = true;
}
try{
Thread.sleep(100);
}
catch (Exception e){
e.printStackTrace();
}
}
}
}
}
hiwaa at 2007-7-16 13:04:28 >

