clone()方法负责建立正确的存储容量,并通过“按位复制”将 二进制位从原始对象中复制到新对象的存储空间
Thread
方法1:继承thread类,但是不能继承其他类,可扩展性低
public class SimpleThread extends Thread { private int countDown = 5; private int threadNumber; private static int threadCount = 0; public SimpleThread() { threadNumber = ++threadCount; System.out.println("Making " + threadNumber); } public void run() { //执行函数 while(true) { System.out.println("Thread " + threadNumber + "(" + countDown + ")"); if(--countDown == 0) return; } } public static void main(String[] args) { for(int i = 0; i < 5; i++) new SimpleThread().start();//调用start System.out.println("All Threads Started"); } } ///:~
方法2:实现Runnable接口
public class MyThread implements Runnable { public void run() { System.out.println("I'm running!"); } }
MyThread tt = new MyThread();
Thread t = new Thread(tt);
t.start();
线程的状态(State)
新生状态(New): 当一个线程的实例被创建即使用new关键字和Thread类或其子类创建一个线程对象后,此时该线程处于新生(new)状态,处于新生状态的线程有自己的内存空间,但该线程并没有运行,此时线程还不是活着的(not alive);
就绪状态(Runnable): 通过调用线程实例的start()方法来启动线程使线程进入就绪状态(runnable);处于就绪状态的线程已经具备了运行条件,但还没有被分配到CPU即不一定会被立即执行,此时处于线程就绪队列,等待系统为其分配CPCU,等待状态并不是执行状态; 此时线程是活着的(alive);
运行状态(Running): 一旦获取CPU(被JVM选中),线程就进入运行(running)状态,线程的run()方法才开始被执行;在运行状态的线程执行自己的run()方法中的操作,直到调用其他的方法而终止、或者等待某种资源而阻塞、或者完成任务而死亡;如果在给定的时间片内没有执行结束,就会被系统给换下来回到线程的等待状态;此时线程是活着的(alive);
阻塞状态(Blocked):通过调用join()、sleep()、wait()或者资源被暂用使线程处于阻塞(blocked)状态;处于Blocking状态的线程仍然是活着的(alive)
死亡状态(Dead):当一个线程的run()方法运行完毕或被中断或被异常退出,该线程到达死亡(dead)状态。此时可能仍然存在一个该Thread的实例对象,当该Thready已经不可能在被作为一个可被独立执行的线程对待了,线程的独立的call stack已经被dissolved。一旦某一线程进入Dead状态,他就再也不能进入一个独立线程的生命周期了。对于一个处于Dead状态的线程调用start()方法,会出现一个运行期(runtime exception)的异常;处于Dead状态的线程不是活着的(not alive)。
Ø线程的方法(Method)、属性(Property)
每个类都有自己的优先级,一般property用1-10的整数表示,默认优先级是5,优先级最高是10;优先级高的线程并不一定比优先级低的线程执行的机会高,只是执行的机率高;默认一个线程的优先级和创建他的线程优先级相同;
2)Thread.sleep()/sleep(long millis)
当前线程睡眠/millis的时间(millis指定睡眠时间是其最小的不执行时间,因为sleep(millis)休眠到达后,无法保证会被JVM立即调度);sleep()是一个静态方法(static method) ,所以他不会停止其他的线程也处于休眠状态;线程sleep()时不会失去拥有的对象锁。 作用:保持对象锁,让出CPU,调用目的是不让当前线程独自霸占该进程所获取的CPU资源,以留一定的时间给其他线程执行的机会;
让出CPU的使用权,给其他线程执行机会、让同等优先权的线程运行(但并不保证当前线程会被JVM再次调度、使该线程重新进入Running状态),如果没有同等优先权的线程,那么yield()方法将不会起作用。
当一个线程执行到wait()方法时,他就进入到一个和该对象相关的等待池(Waiting Pool)中,同时失去了对象的机锁—暂时的,wait后还要返还对象锁。当前线程必须拥有当前对象的锁,如果当前线程不是此锁的拥有者,会抛出IllegalMonitorStateException异常,所以wait()必须在synchronized block中调用。
唤醒在当前对象等待池中等待的第一个线程/所有线程。notify()/notifyAll()也必须拥有相同对象锁,否则也会抛出IllegalMonitorStateException异常。
synchronized
一在任何时刻,只可有一个线程调用特定对象的一个 synchronized方法。
synchronized方法: synchronized void f() { /* ... */ }
每个对象都包含了一把锁(也叫作“监视器”),它自动成为对象的一部分(不必为此写任何特殊的代 码)。
调用任何synchronized方法时,对象就会被锁定,不可再调用那个对象的其他任何synchronized方法.
synchronized代码块
synchronized(syncObject) {
// This code can be accessed by on ly
// one thread at a time, assuming all
// threads respect syncObject's lock
}
在能进入同步块之前,必须在 synchObject 上取得锁。如果已有其他线程取得了这把锁,块便不能进入,必 须等候那把锁被释放
wait, notify, notifyAll
public class coffee { public coffee(){ String lock = "lock"; mythread my = new mythread(lock, 1); mythread my1 = new mythread(lock, 2); runthread run = new runthread(lock, 1); my.start(); my1.start(); Thread thread = new Thread(run); try { Thread.sleep(1000); } catch (InterruptedException ex) { Logger.getLogger(coffee.class.getName()).log(Level.SEVERE, null, ex); } thread.start(); } } class mythread extends Thread{ String lock; int ithread; public mythread(String lock, int i){ this.lock = lock; this.ithread = i; } public void run(){ String s = "Run mythread "+this.ithread; System.out.println(s); synchronized(lock){ try { s = "mythread "+this.ithread +" wait"; System.out.println(s); lock.wait(); s = "mythread "+this.ithread +" going on"; System.out.println(s); //lock.notify(); } catch (InterruptedException ex) { Logger.getLogger(mythread.class.getName()).log(Level.SEVERE, null, ex); } } } } class runthread implements Runnable{ String lock; int ithread; public runthread(String lock, int i){ this.lock = lock; this.ithread = i; } public void run(){ String s = "Run runthread "+this.ithread; System.out.println(s); synchronized(lock){ s = "runthread "+this.ithread +" notify"; System.out.println(s); lock.notifyAll();//通知所有lock.wait的线程可以继续执行了 //lock.notify();随即的一个线程的wait解除 } } }
run:
Run mythread 2
Run mythread 1
mythread 2 wait
mythread 1 wait
Run runthread 1
runthread 1 notify
mythread 1 going on
mythread 2 going on