lpr-b:lpr-b-09:soluzioni
Differenze
Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.
Entrambe le parti precedenti la revisioneRevisione precedenteProssima revisione | Revisione precedente | ||
lpr-b:lpr-b-09:soluzioni [22/10/2009 alle 20:18 (16 anni fa)] – Andrea Corradini | lpr-b:lpr-b-09:soluzioni [22/10/2009 alle 20:42 (16 anni fa)] (versione attuale) – Andrea Corradini | ||
---|---|---|---|
Linea 5: | Linea 5: | ||
[[FAQ|FAQ]] | [[FAQ|FAQ]] | ||
- | ===== Tasks e Threads | + | |
+ | ====== Tasks e Threads | ||
+ | |||
+ | ===== PrintTask | ||
< | < | ||
Linea 57: | Linea 60: | ||
</ | </ | ||
- | ===== Tasks e Threads Es 2 ===== | + | ===== SleepTask |
< | < | ||
Linea 99: | Linea 102: | ||
</ | </ | ||
- | ====== Tasks e Threads ====== | + | ===== PITask2 |
- | ===== Tasks e Threads Es 3 ===== | + | |
< | < | ||
- | public class PITask | + | public class PITask2 |
{ | { | ||
private double piEstimate = 0.0; | private double piEstimate = 0.0; | ||
Linea 162: | Linea 164: | ||
====== Thread Pool ====== | ====== Thread Pool ====== | ||
- | ===== Thread Pool Es 1 ===== | + | ===== AsynchronousCalculator |
< | < | ||
Linea 240: | Linea 242: | ||
System.out.println(" | System.out.println(" | ||
} | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Task ===== | ||
+ | |||
+ | < | ||
+ | import java.text.SimpleDateFormat; | ||
+ | import java.util.Date; | ||
+ | |||
+ | public abstract class Task implements Runnable | ||
+ | { | ||
+ | public long creationTime; | ||
+ | public long startTime; | ||
+ | public long endTime; | ||
+ | public String result; | ||
+ | |||
+ | public Task() | ||
+ | { | ||
+ | this.creationTime = System.currentTimeMillis(); | ||
+ | } | ||
+ | |||
+ | public abstract void compute(); | ||
+ | |||
+ | public void setResult(String result) | ||
+ | { | ||
+ | this.result = result; | ||
+ | } | ||
+ | |||
+ | public void run() | ||
+ | { | ||
+ | this.startTime = System.currentTimeMillis(); | ||
+ | compute(); | ||
+ | this.endTime = System.currentTimeMillis(); | ||
+ | printResult(); | ||
+ | } | ||
+ | |||
+ | public void printResult() | ||
+ | { | ||
+ | SimpleDateFormat formatter = new SimpleDateFormat(" | ||
+ | String creationString = formatter.format(new Date(this.creationTime)); | ||
+ | String startString = formatter.format(new Date(this.startTime)); | ||
+ | String endString = formatter.format(new Date(this.endTime)); | ||
+ | |||
+ | System.out.println(Thread.currentThread().getName() | ||
+ | + ", creationTime = " + creationString + ", startTime = " | ||
+ | + startString + ", endTime = " + endString + ", " + result); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== PITask ===== | ||
+ | |||
+ | < | ||
+ | public class PITask extends Task | ||
+ | { | ||
+ | private double piEstimate = 0.0; | ||
+ | private long iteration = 0; | ||
+ | private int sign = -1; | ||
+ | private double accuracy; | ||
+ | |||
+ | public PITask(int precision) | ||
+ | { | ||
+ | this.accuracy = Math.pow(10, | ||
+ | } | ||
+ | |||
+ | public void compute() | ||
+ | { | ||
+ | while(Math.abs(piEstimate - Math.PI) > accuracy) | ||
+ | { | ||
+ | iteration++; | ||
+ | sign = -sign; | ||
+ | piEstimate += sign * 4.0 / ((2 * iteration) - 1); | ||
+ | } | ||
+ | setResult(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ===== FibTask ===== | ||
+ | |||
+ | < | ||
+ | public class FibTask extends Task | ||
+ | { | ||
+ | int value; | ||
+ | |||
+ | public FibTask(int value) | ||
+ | { | ||
+ | this.value = value; | ||
+ | } | ||
+ | |||
+ | private int fib(int n) | ||
+ | { | ||
+ | if(n == 0 || n == 1) return n; | ||
+ | if(n == 1) return 1; | ||
+ | return fib(n - 1) + fib(n - 2); | ||
+ | } | ||
+ | |||
+ | public void compute() | ||
+ | { | ||
+ | int result = this.fib(value); | ||
+ | setResult(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== FactTask ===== | ||
+ | |||
+ | < | ||
+ | public class FactTask extends Task | ||
+ | { | ||
+ | int value; | ||
+ | |||
+ | public FactTask(int value) | ||
+ | { | ||
+ | this.value = value; | ||
+ | } | ||
+ | |||
+ | private long fact(int n) | ||
+ | { | ||
+ | if(n == 0) return 1; | ||
+ | if(n == 1) return 1; | ||
+ | return(n * fact(n - 1)); | ||
+ | } | ||
+ | |||
+ | public void compute() | ||
+ | { | ||
+ | long result = this.fact(value); | ||
+ | setResult(" | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Indirizzi IP ====== | ||
+ | |||
+ | ===== ListInterface ===== | ||
+ | < | ||
+ | import java.util.Enumeration; | ||
+ | import java.net.*; | ||
+ | |||
+ | public class ListInterface | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | try | ||
+ | { | ||
+ | Enumeration< | ||
+ | if(interfaceList == null) | ||
+ | { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | while(interfaceList.hasMoreElements()) | ||
+ | { | ||
+ | NetworkInterface iface = interfaceList.nextElement(); | ||
+ | System.out.println(" | ||
+ | Enumeration< | ||
+ | while(addrList.hasMoreElements()) | ||
+ | { | ||
+ | InetAddress address = addrList.nextElement(); | ||
+ | System.out.print(" | ||
+ | | ||
+ | System.out.println(": | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | }catch(SocketException e) { System.out.println(e); | ||
+ | } | ||
} | } | ||
</ | </ |
lpr-b/lpr-b-09/soluzioni.1256242703.txt.gz · Ultima modifica: 22/10/2009 alle 20:18 (16 anni fa) da Andrea Corradini