OCJP题库知识点总结(2)

来源:互联网 发布:文档归类软件 编辑:程序博客网 时间:2024/04/29 00:26

public class Threads56 implements Runnable {

 @Override public void run() {  // TODO Auto-generated method stub  System.out.println("run"); }

 public static void main(String[] args) {  // TODO Auto-generated method stub  Thread t = new Thread(new Threads56());  t.start();  System.out.println("main end"); }

}

上面的执行结果其实是不定的,有时候是

run

main end

有时候结果是

main end

run

如果要求main等待跟昨天一样需要在t.start()后加上t.join();

 

package com.liu3;public class Test57 {public static void main(String[] args) {// TODO Auto-generated method stubint i = 100;System.out.printf("%d\n",i); //10进制整数System.out.printf("%o\n",i); //8进制整数System.out.printf("%x\n",i); //16进制整数System.out.printf("%h\n",i); //16进制字符串System.out.printf("%f\n",Math.PI); //10进制浮点数System.out.printf("%b\n",Math.PI); //逻辑型System.out.printf("%s\n",Math.PI); //字符串System.out.printf("%e\n",Math.PI); //科学计数法}}


 

格式输出控制符还记得吗

 

package com.liu3;public class Test63 {public static void main(String[] args) {// TODO Auto-generated method stubInteger x =400;Integer z =400;Integer y = x;x++;System.out.println(x==y);System.out.println(z==y);System.out.println(z.equals(y));}}


 

x==y false,z==y false,z.equals(y) true

不得不说这里有个大陷阱,如果x和z在-128到127之间呢?这个时候z==y true

请看下面的方法,如果值在-128到127之间直接从缓存里读取对象,范围外才会新建一个对象

    public static Integer valueOf(int i) {        if (i >= IntegerCache.low && i <= IntegerCache.high)            return IntegerCache.cache[i + (-IntegerCache.low)];        return new Integer(i);    }

String 对象同样具有一样的缓存池

 

关于Thread的一些方法

1>sleep() 在指定的毫秒数内让当前正在执行的线程休眠 Thread类的方法 不释放锁

2>wait()  当前线程暂停,并释放锁

3>notify()/notifyAll()  唤醒一个处于等待锁的线程,然后继续往下执行

4>yield() 当前线程暂停,执行其他线程

 

public class Thread001 extends Thread {public String lock;@Overridepublic void run() {synchronized (lock){System.out.println(Thread.currentThread().getName()+"begin wait");try {lock.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName()+"end wait");}}}public class Thread002 extends Thread {public String lock;@Overridepublic void run() {synchronized (lock){System.out.println(Thread.currentThread().getName()+"begin notify");lock.notifyAll();System.out.println(Thread.currentThread().getName()+"end notify");}}}public class Thread003 extends Thread {public String lock;@Overridepublic void run() {synchronized (lock){System.out.println(Thread.currentThread().getName()+"begin wait");try {lock.wait();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(Thread.currentThread().getName()+"end wait");}}}public class ThreadMain {public static void main(String[] args){String lock="lock";Thread001 t1 = new Thread001();Thread002 t2 = new Thread002();Thread003 t3 = new Thread003();t1.lock = lock;t2.lock = lock;t3.lock = lock;t1.start();t3.start();t2.start();}}


下面代码的运行结果是不定的。如果想输出7-1 7-2 8-1 8-2的形式应该怎么办呢

package com.liu3;public class PingPong implements Runnable {@Overridepublic void run() {hit(Thread.currentThread().getId());}synchronized void hit(long n) { for(int i = 1; i < 3; i++) System.out.print(n + "-" + i + " ");}public static void main(String[] args) {// TODO Auto-generated method stub new Thread(new PingPong()).start(); new Thread(new PingPong()).start();}}

如下修改即可

public static void main(String[] args) {// TODO Auto-generated method stub PingPong p = new PingPong(); new Thread(p).start(); new Thread(p).start();}


调用构造器方法要用this(),类方法可以和构造器方法同名

package com.liu3;public class Hello {public Hello(){System.out.println("constructor");}public void Hello(){System.out.println("method");}public Hello(int a){this();Hello();}public static void main(String[] args) {// TODO Auto-generated method stubHello he = new Hello(5);}}




 

0 0