复习java4

xiaoxiao2021-02-27  244

package com.company; /** * Created by henson on 17-8-30. */ public class Train { public static void main(String[] args){ /*Thread t1=new Thread(new Runner3(),"售票窗口1 : "); Thread t2=new Thread(new Runner3(),"售票窗口2 : "); Thread t3=new Thread(new Runner3(),"售票窗口3 : "); Thread t4=new Thread(new Runner3(),"售票窗口4 : "); Thread t5=new Thread(new Runner3(),"售票窗口5 : "); t1.start(); t2.start(); t3.start(); t4.start(); t5.start(); */ for (int j = 1; j <=5; j++) { new Thread(new Runner3(),"售票点 " + j).start(); } } } class Runner3 implements Runnable{ public static int count; public Runner3(){int count=1;} public void run(){ long startTime=System.currentTimeMillis(); //获取开始时间 synchronized(this) { while (count<1000){ try { System.out.println(Thread.currentThread().getName() + " 当前票号" + (count++)); Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } long endTime=System.currentTimeMillis(); //获取结束时间 System.out.println("程序运行时间: "+(endTime-startTime)+"ms"); } // public int getCount() { // return count; //} } package com.company; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; /** * Created by henson on 17-8-30. */ public class FileCopyByChar { public static void main(String[] args){ try { FileReader reader =new FileReader("/home/henson/Desktop/SHS4N.csv"); FileWriter writer=new FileWriter("Copy.txt"); int c=reader.read(); while (c!=-1){ writer.write(c); System.out.print((char)c); //新认识 c=reader.read(); //所谓温故而知新 } //开心 reader.close(); writer.close(); }catch (IOException e){ System.out.println(e); } } } package com.company; import java.io.*; /** * Created by henson on 17-8-30. */ public class CopyFileAddLineNumber { public static void main(String[] args){ String in="/home/henson/Desktop/SHS4N.csv"; String out="copy3.txt"; if (args.length>=1) in=args[0]; if (args.length>=2) out=args[1]; try { File fin=new File(in); File fout=new File(out); BufferedReader br=new BufferedReader(new FileReader(fin)); PrintWriter pw=new PrintWriter(new FileWriter(fout)); int cnt=0; String s=br.readLine(); while (s!=null){ cnt++; s=del(s); pw.write(s+"\n"); System.out.println(cnt+":\t"+s); s=br.readLine(); } br.close(); pw.close(); }catch (IOException e){System.out.println(e);} } static String del(String s){ if (s==null) return s; int pos=s.indexOf("//"); if (pos<0) return s; return s.substring(0,pos); } } package com.company; import java.io.*; /** * Created by henson on 17-8-30. */ public class FileCopyByLine { public static void main(String[] args){ try { FileReader reader=new FileReader("/home/henson/Desktop/SHS4N.csv"); BufferedReader br=new BufferedReader(reader); FileWriter writer=new FileWriter("copy2.txt"); BufferedWriter bw=new BufferedWriter(writer); String s=br.readLine(); while (s!=null){ bw.write(s); bw.newLine(); System.out.println(s); s=br.readLine(); } br.close(); bw.close(); }catch (IOException e){ System.out.println(e); } } }

温故而知新是真的,新认识,开心。 无聊的我,试了1,2,4,8,16,20,,,,,100,1000,10000的线程的区别, 得出的结论大概是,线程数最好为cup核数的2-8倍,线程数太多,更替频率过高,会导致内存开销很大,且体现不出多线程的优势。

转载请注明原文地址: https://www.6miu.com/read-10979.html

最新回复(0)