标签:
方法 | 描述 |
---|---|
static int abs(int a) | 获取绝对值 |
static double ceil(double a) | 向上取整 |
static double floor(double a) | 向下取整 |
static float max(float a,float b) | 获取两个数值中的最大值 |
static double scalb(double a,int b) | a的b次幂 |
static double random() | 产生随机数,线程安全同步 |
static double sqrt(double a) | 正平方根 |
static int round(float a) | 四舍五入 |
1 2 | Random() //默认种子,每次产生的随机数不同 Random( long seed) //指定种子,每次种子相同,随机数就相同 |
方法 | 描述 |
---|---|
int nextInt() | 返回int范围内的随机数 |
int nextInt(int n) | 返回[0,n)范围内的随机数 |
方法 | 描述 |
---|---|
static void gc() | 运行垃圾回收器 |
static void exit(int status) | 退出jvm |
static long currentTimeMillis() | 获取当前时间的毫秒值 |
static void arraycopy (Object src,int srcPos,Object dest,int destPos,int length) | 数组复制 |
1 2 3 4 | import java.util.Date; import java.text.SimpleDateFormat; SimpleDateFormat df = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); //设置日期格式 string currentTime=df.format( new Date()); |
1 | Calendar c = Calendar.getInstance(); //可以对每个时间域单独修改,获取当前系统时间 |
1 2 3 4 5 6 7 | int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int date = c.get(Calendar.DATE); int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); System.out.println(year + "/" + month + "/" + date + " " +hour + ":" +minute + ":" + second); |
1 2 3 4 5 6 7 8 9 10 | SimpleDateFormat sdf= new SimpleDateFormat( "yyyy-MM-dd" ); java.util.Date time= null ; try { time= sdf.parse(sdf.format( new Date())); } catch (ParseException e) { e.printStackTrace(); } |
1 2 3 4 | String regex = "[0-9]+" ; //书写正则表达式 Pattern p = Pattern.compile(regex); //加载到编译模式 Matcher m = p.matcher(s); //匹配 boolean b = m.matches(); //是否匹配 |
1 2 3 | String input = "028-10234567" ; string regex = "[0]{1}\\d{2}-[1-9]{1}\\d{7}|[0]{1}\\d{3}-[1-9]{1}\\d{6}" ; boolean b = input.matches(regex); |
1 | Thread t=Thread.currentThread( ) //获取当前线程对象 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Ticket1 extends Thread { int num = 50 ; public Ticket1(String name) { super (name); } //重写父类的构造函数 public void run() { for ( int i = 0 ; i < 200 ; i++) { if (num > 0 ) { System.out.println(getName()+ "卖出第" +num-- + "张" ); } } } |
1 | new Ticket1( "A" ).start(); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | class Ticket2 extends Object implements Runnable { int num = 50 ; @Override public void run() { for ( int i = 0 ; i < 200 ; i++) { if (num > 0 ) { System.out.println(Thread.currentThread().getName()+ "卖出第" +num-- + "张" ); } } } } |
1 2 | Runnable target = new Ticket2(); new Thread(target, "A" ).start(); //多态子类指向父类接口 |
1 | Thread对象.setDaemon( true ); |
1 | boolean isAlive() //来判断线程是否活的 |
1 2 | Thread t = new Thread( new Join()); t.join() //强制执行线程,执行到这里的话,等待t执行完主线程才会执行 |
1 2 | setPriority( int x) getPriority() |
1 2 3 | MAX_PRIORITY //值是10 MIN_PRIORITY //值是1 NORM_PRIORITY //值是5(主方法默认优先级) |
1 | Thread.yield() //暂停该线程让别的线程先执行,然后这个线程进入就绪状态 |
1 2 3 4 | synchronized (obj) { 同步代码 } |
Java基础学习笔记【05】Math、Random、System、时间格式化、多线程
标签:
原文地址:http://www.cnblogs.com/lindongdong/p/3addb409865c6e3327504299225df30c.html