Network Programming udp operation learning notes, udp learning notes
The core is about idempotent operation learning.
package com.udp;public class ThreadRun implements Runnable { private boolean flag = true; @Override public void run() { for (int i = 0 ; i< Integer.MAX_VALUE && flag ; i++){ System.out.println("i = " + i); } } public void cancel(){ flag = false; }}
Package com. udp; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. util. timer; import java. util. timerTask;/** idempotent operations, A ^ n = A, idempotent operations, A method is called multiple times, just like a method called once */public class suplement {public static void main (String [] args) {/* // a method is called multiple times. It is the same as a method called once. ThreadRun threadRun = new ThreadRun (); new Thread (threadRun ). start (); new Timer (). schedule (new TimerTask () {@ Override public void run () {threadRun. cancel () ;}}, 3000); * // try-with-resouce try (// The method in applying for resource Auto Closeable is a non-idempotent operation, so either try_with_resource FileOutputStream fos = new FileOutputStream ("a.txt") // only the closeable or AutoClosesable subclass can be placed in parentheses. // int I = 0;) {fos. write ("hello ". getBytes ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {// either close the stream resource here. // select either of them }}}