Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display my first name and last name using 2 threads. Once user click ctrl with some key programe should be terminate. Please advice me to begin this. here is my code and it is display only one tome. but i want to display this into until user press some key.
Java
 class Thread_Ex11{
public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "gayan");
        Thread t2 = new Thread(new MyRunnable(), "suranga");
        t1.start();
        try {
            t1.join(2000);
            t2.join(2000); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
          
            t2.start();
            t1.start();  

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
      
        }

}

class MyRunnable implements Runnable{

    @Override
    public void run() {
       while(true){
        System.out.println(Thread.currentThread().getName());
             
       }  
  }
    
}


What I have tried:

my out put of this programme compiling
this is one
this is two

but i want out put is
this is one
this is two
this is one
this is two
etc....
terminated
Posted
Updated 29-Sep-16 23:25pm
v4

You need to add loops into your two threads so they keep printing the same text over and over. You may also need to add some form of delay so they do not run away with the system. And in your main method after starting the threads it needs to wait for a keypress or console input. At that point it should terminate the two threads.
 
Share this answer
 
Comments
nuke_infer 30-Sep-16 4:41am    
yes i insert the while loop to execute the result. here the code that i inserted while loop
class Thread_Ex11{
public static void main(String[] args) {
Thread t1 = new Thread(new MyRunnable(), "one");
Thread t2 = new Thread(new MyRunnable(), "two");
t1.start();
try {
t1.join(2000);
t2.join(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
t2.start();
t1.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class MyRunnable implements Runnable{
@Override
public void run() {
while(true){
System.out.println(Thread.currentThread().getName());
}
}
}
out put is showing
one,one, one,one,one,two,two,two,one,two,etc...

but i want answer is
one,two,one,two,one,two,etc...
Richard MacCutchan 30-Sep-16 4:48am    
Sorry I do not understand the question. Threads run independently so the order of any output from them will be random.

When adding details about your code please edit your question and add it there.
nuke_infer 30-Sep-16 5:09am    
please check it now. i have edited the above code.actual result and expected result of my code.
Richard MacCutchan 30-Sep-16 5:25am    
See my new solution below
Try this, it works for me.
Java
public class Thread_Ex {
    public static void main(String [] args) {
        Thread t1 = new Thread(new MyRunnable(), "gayan");
        Thread t2 = new Thread(new MyRunnable(), "suranga");
        
        t1.start();
        t2.start();
        while (true) {
            try {
                System.in.read();
                t1.interrupt();
                t2.interrupt();
            }
            catch (Exception eee) {
                //
            }
        }
    }
}

class MyRunnable implements Runnable {
    @Override
    public void run() {
        while(true) {
            System.out.println(Thread.currentThread().getName());
            try {
                Thread.sleep(200);
            }
            catch (InterruptedException zz) {
                //
            }
            
            if (Thread.interrupted()) {
                System.out.println("interrupted");
                break;
            }
        } 
    }
}
 
Share this answer
 
v2
Comments
nuke_infer 30-Sep-16 5:50am    
this is ok but it is printing both values together.i want one after other one.
Richard MacCutchan 30-Sep-16 5:52am    
Then you need to figure out some sort of synchronisation between the two threads. Maybe a mutex or semaphore (Google will find you examples).
nuke_infer 30-Sep-16 6:15am    
thank you richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900