| |

VerySource

 Forgot password?
 Register
Search
View: 813|Reply: 3

About timing in java

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-24 16:20:01
| Show all posts |Read mode
Talk about the process:
When a datagram DatagramPacket is ready, it is sent using DatagramSocket.send ().
Problem occurred:
At this time, we need to start a timer. When the timer expires, we have not received the feedback from the datagram, then we can guess: the receiver did not receive the datagram we sent. Then we will automatically resend a datagram.
The question is how to implement this timer.

If you use the Timer class to implement, then the run () method rewritten in TimerTask must be static code (this should be the case), then there is no way to resend this DatagramPacket, because it is not final.

Excuse me, heroes, how to solve this problem?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-11 19:15:01
| Show all posts
Don't fully understand what you mean.

Start the timer at the same time that the data is sent, and perform the timeout after the specified time (resend the data); if there is a timely feedback, stop the timer (to prevent repeated sending of data).
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-2-13 16:45:01
| Show all posts
That's right. If you receive feedback, then stop the timer. For example, use the cancel method of the Timer class.
If there is no feedback, then the datagram should be resent ... but the run method in the TimerTask class seems not suitable for resending the datagram, because this DatagramSocket and DatagramPacket are not final
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-2-15 13:00:01
| Show all posts
This has nothing to do with whether it is final? Don't know how your code is written

Your DatagramSocket and DatagramPacket objects are passed as method parameters and these two parameters are passed into the anonymous class defined in the method? If so, then this is fine (add final to the method parameter declaration):
public void aMethod (final DatagramSocket ds, final DatagramPacket dp) {
    Timer timer = new Timer ();
    timer.schedule (new TimerTask () {
        public void run () {
            ds.send (dp);
        }
    }, 10 * 1000);
}
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list