Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

java thread  RSS feed

 
gayan suranga
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • 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)
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
now this is printing without getting expected order.please help me to arrange an orderly with some time period
 
Norm Radder
Ranch Foreman
Posts: 2216
28
  • Mark post as helpful
  • send pies
  • 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?
 
Santosh Kumar Nayak
Ranch Hand
Posts: 193
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!