Question
Please help me to solve this problem with java language!
write a program that runs a thread which prints a timestamp and then waits a second by doing the following: 1. Implement a cl
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

import java.util.Date;

public class ThreadEx implements Runnable
{
public static void main(String args[])
{
ThreadEx runnable=new ThreadEx();   
Thread t=new Thread(runnable);
t.start();
}

public void run()
{
try{   
   java.util.Date date=new java.util.Date();
System.out.println(date);
Thread.sleep(500);   
}
  
catch(InterruptedException e)  
{
Thread.currentThread().interrupt();
System.out.println("Thread Interrupted");
}
}
}

Code is explained below:

​​​// Implementing the Runnable interface 1 import java.util.Date; 2 public class ThreadEx implements Runnable 3-4 public static

OUTPUT:

Sun Dec 08 09:12:37 UTC 2019 1... Program finished with exit code o Press ENTER to exit console. I

Add a comment
Know the answer?
Add Answer to:
Please help me to solve this problem with java language! write a program that runs a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please use java Language Part I Implement a thread by extending the Thread class. Your thread...

    Please use java Language Part I Implement a thread by extending the Thread class. Your thread must do the following: (25 pts.) • Print its name and ID at the start. • Print the numbers from 1 to 100 that can be divided by 5. • Wait 1 second before printing a number. Part II Implement a thread by implementing the Runnable interface. Your thread must do the following: (25 pts.) • Print its name and ID at the start....

  • 1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System....

    1. Analyze the following code: public class Test implements Runnable { public static void main(String[] args) { Thread t = new Thread(this); t.start(); } public void run() { System.out.println("test"); } } 1. The code compiles but will not print anything since t does not invoke the run method. 2. The code will not compile since you cannot invoke "this" in a static method. 3. The program compiles, runs, and prints tests on the console. 2. What will the following example...

  • Can some please help me to answer these two questions. Write a program in Java create...

    Can some please help me to answer these two questions. Write a program in Java create a class diagram fragment with two classes: User and Patient. Patient is a sub-class of User. User has a method getName(), which is both over-loaded and over-ridden in the subclass In Java write an abstract class called User. User has one abstract method called authenticate, which takes two string parameters. Write a concrete sub-class of User called Administrator with an implementation of the authenticate...

  • Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both...

    Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...

  • Write a program that will define a runnable frame to have a ball move across the...

    Write a program that will define a runnable frame to have a ball move across the frame left to right. While that is happening, have another frame that will let the user do something like click a button or input into a textfield, but keep the ball moving in the other frame. When the ball reaches the right edge of the frame or when the user takes action - enters in textfield or clicks the button, end the thread. The...

  • Please help with Java test questions, I will post the rest of the question in separate...

    Please help with Java test questions, I will post the rest of the question in separate post. Question 1 Consider the following program: public class CountDownApp3 {     public static void main(String[] args)     {         Thread t1 = new CountDown();         Thread t2 = new CountDown();         t1.start();         t2.start();     } } Assuming that the CountDown class extends the Thread class, how many threads are used to run this program? a. 3 b. 2 c. 4 d. 1...

  • Need some help on the following JAVA problem: Write a program in which the main thread...

    Need some help on the following JAVA problem: Write a program in which the main thread creates two threads named “Display” and “Daemon”. The first thread (Display) executes in an infinite loop and prints out its name and system time every 1 second until it is interrupted by the main thread. The second thread is a daemon thread that executes in an infinite loop and prints out its name. The main thread waits for a specific number of seconds n...

  • According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord...

    According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. According to this cord use LongFifoWait Notify thread methord in CircularArrayLongFifo class. public class CandidateGenerator { private final LongFifo outputFifo; public CandidateGenerator(LongFifo outputFifo) { this.outputFifo = outputFifo; Runnable r = new Runnable() { @Override public void run() { runWork();}}; Thread t = new Thread(r, "CandidateGenerator"); t.start() } private void runWork() { try { outputFifo.add(2); long number = 3; while ( true ) { outputFifo.add(number); number += 2;} }...

  • Can someone please help with this in JAVA? Write one application program by using the following...

    Can someone please help with this in JAVA? Write one application program by using the following requirements: 1) Exception handling 2) Inheritance a) At least one superclass or abstract superclass: this class needs to have data members, accessor, mutator, and toString methods b) At least one subclass: this class also needs to have data members, accessor, mutator, and toString methods c) At least one interface: this interface needs to have at least two abstract methods d) At least one method...

  • my goal is to generate a random number every 10 seconds and store in arraylist. when...

    my goal is to generate a random number every 10 seconds and store in arraylist. when I generate the new number every 10 second I want to update the previous number from the arraylist. currently it gets the first current number in main method but when I store after 10 second i'm not getting the update current number. I would appreciate any help. NOTE: ***I want the Runnable to be in the same class, Not in main method. my goal...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT