随笔分类 -  编程技巧

四则运算
摘要:public class calc{public String oper(float a,float b,char c){float add=a+b;float jian=a-b;float chen=a*b;float chu=a/b;switch(c){case '+':return "想加的结果为:"+add;case '-':return "想减的结果为:"+jian;case '*':return "想乘的结果为:"+chen;case '/':return & 阅读全文
posted @ 2012-02-29 22:23 平安夜 阅读(112) 评论(0) 推荐(0)
俩个线程一个加1,一个减1
摘要:public class ThreadTest1 {private int j;public static void main(String args[]){ThreadTest1 tt=new ThreadTest1();Inc inc=tt.new Inc();Dec dec=tt.new Dec();for(int i=0;i<2;i++){Thread t= new Thread(inc);t.start();t=new Thread(dec);t.start();}}private synchronized void inc(){j++;System.out.println(T 阅读全文
posted @ 2012-01-26 01:12 平安夜 阅读(585) 评论(0) 推荐(0)
线程范围内的数据共享
摘要:线程范围内使用自己独立的数据:import java.util.Random;public class ThreadScopeShareData{private static int data=0;private static Map<Thread,Integer> threadData=new HashMap<Thread,Integer>();public static void main(String[] args){for(int i=0;i<2;i++){new Thread(new Runnable(){@Overridepublic void run 阅读全文
posted @ 2012-01-25 23:56 平安夜 阅读(299) 评论(0) 推荐(0)
定时器
摘要:import java.util.Timer;import java.util.TimerTask;import java.util.Date;public class jzw{public static void main(String[] args){new Timer().schedule(new TimerTask(){@Overridepublic void run(){System.out.println("Bombing!");}},10000,3000);//过3秒炸一下//10秒炸一下/*while(true){System.out.println(new 阅读全文
posted @ 2012-01-24 13:21 平安夜 阅读(202) 评论(0) 推荐(0)
线程(俩种实现方式)
摘要:线程一个通过继承Thread来实现,一个通过实现Runnable接口的Run方法来实现public class First{public static void main(String[] args){Thread thread =new Thread(){@Overridepublic void run(){while(true){try{Thread.sleep(1000);}catch(InterruptedException e){e.printStackTrace();}System.out.println("1:"+Thread.currentThread(). 阅读全文
posted @ 2012-01-24 12:45 平安夜 阅读(241) 评论(0) 推荐(0)