• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java thread

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Heloo friends. I need advice for my assignment. My problem is 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.

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

gayan suranga wrote:Once user click ctrl with some key programe should be terminate....but i want to display this into until user press some key.



Let's break the problem into 2 smaller problems
1) Make the thread print the value till the user presses a specific key
which means make your code
2) listen to the keyboard and check if the specific key was pressed, if yes, exit

Have you learnt loops? A while loop is exactly what you need for #1
Recommended reading: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

For #2 you can use the Scanner class.
More on it here https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html

The standard way of terminating the application is via System#exit(int status);
 
gayan suranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes iam familier with the scanner class and while loop. but i want implement this into thread
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

gayan suranga wrote:yes iam familier with the scanner class and while loop. but i want implement this into thread


So what have you tried so far and where are you stuck? The code you posted does not use any while loop or scanner
 
gayan suranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without using any looping cant we use thread to continuously diplay the expected output.like
this is one, this is two, this is one,etc...
 
Maneesh Godbole
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.

If you read the link I shared, you will see it is very easy to run the while loop (or any other loop for that matter)
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two threads kept printing something on the command prompt and If we want to terminate that on a specific key press event then I think we need GUI based program for that using AWT, Swing etc something like that.
 
gayan suranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
now this is printing without getting expected order.please help me to arrange an orderly with some time period
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

help me to arrange an orderly with some time period  


Could you post an example of "orderly"?

What do you mean by "time period"?  Are you asking about having a thread sleep for a period of time each iteration of the loop?
 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you mention the order of Output you want to achieve ?

You can use Thread.sleep() to implement  the same.
 
reply
    Bookmark Topic Watch Topic
  • New Topic