并行指在同一时刻,有多条指令在多个处理器上同时执行;并发指在同一时刻只能有一条指令执行,但多个进程指令被快速轮换执行,使得在宏观上具有多个进程同时进行的效果。对于一个CPU而言,他在某个时间点上只能执行一个程序,也就是说只能运行一个进程,CPU不断地在这些进程之间轮换执行。但是我们感觉不到任何中断现象,原因是CPU的执行速度相对我们的感觉太快了。
创建线程有两种方式,一是继承Thread类,二是实现Runnable接口。如果采用Thread方式,其优势是访问当前线程时,无须使用Thread.currentThread()方法,直接使用this即可获得当前线程;劣势是其已经继承了Thread类,所以不能再继承其他父类。如果采用Runnable方式,其优势是实现Runnable接口的同时,还可以继承其他类,并且在这种方式相爱可以多个线程共享同一个target对象;劣势是访问当前线程,必须使用Thread.currentThread()方法。注:采用Runnable方式创建的对象只能作为线程对象的target。
package wbb.java.Thread;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-11
* Time: 上午9:54
* Thread/Runnable两种方式创建进程
*/
public class ThreadAndRunnable {
public static void main(String[] args) {
ByThread thread = new ByThread();
thread.start();
//采用Runnable方式创建的对象只能作为线程对象的target。
ByRunnable runnable = new ByRunnable();
Thread t = new Thread(runnable);
t.start();
}
}
class ByThread extends Thread {
public void run() {
System.out.println(this.getName());
}
}
class ByRunnable implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
测试某条线程是否已经死亡,可以调用线程对象的isAlive()方法,当线程处于就绪、运行、阻塞三种状态是,该方法将返回true;当线程处于新建、死亡两种状态时,该方法将返回false。警告:1、不要对一个线程调用两次start()方法;2、不要对一个已经死亡的线程调用start()方式使其重新启动。这两种做法都会导致IllegalThreadStateException异常。
package wbb.java.Thread;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-11
* Time: 上午10:11
* 两种错误的做法导致IllegalThreadStateException
*/
public class ThreadException extends Thread {
public void run() {
System.out.println("123");
}
public static void main(String[] args) {
ThreadException thread1 = new ThreadException();
ThreadException thread2 = new ThreadException();
//两次调用start()方法
thread1.start();
thread1.start();
//试图对已经死亡的线程调用start()方法使其启动
if(!thread2.isAlive()) {
thread2.start();
}
}
}
package wbb.java.Thread;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-11
* Time: 上午10:26
* join方法的作用
*/
public class JoinThread extends Thread {
public void run() {
for (int i = 0; i < 10; i++) {
System.out.println(this.getName());
}
}
public static void main(String[] args) throws Exception {
JoinThread thread1 = new JoinThread();
thread1.setName("thread1");
JoinThread thread2 = new JoinThread();
thread2.setName("thread2");
thread1.start();
//保证线程thread1执行完毕后再往下继续执行
thread1.join();
thread2.start();
}
}
如果没有添加join()来保证thrread1已经执行完毕的话,CPU会不停的无规律的轮换执行,所以会出现thread1、thread2交替执行的结果。如图:package wbb.java.Thread;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-11
* Time: 上午11:21
*/
public class SleepThread extends Thread {
public SleepThread(String name) {
super(name);
}
public void run() {
for(int i=0;i<10;i++) {
System.out.println(this.getName() + i);
if(i == 3) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
SleepThread sleep1 = new SleepThread("Sleep-高优先级");
SleepThread sleep2 = new SleepThread("Sleep-低优先级");
sleep1.setPriority(Thread.MAX_PRIORITY);
sleep2.setPriority(Thread.MIN_PRIORITY);
sleep1.start();
sleep2.start();
}
}
运行效果如图,可以看出sleep方法对不会理会优先级的高低。package wbb.java.Thread;
/**
* Created with IntelliJ IDEA.
* User: wbb
* Date: 14-7-11
* Time: 上午11:25
* To change this template use File | Settings | File Templates.
*/
public class YieldThread extends Thread {
public YieldThread(String name) {
super(name);
}
public void run() {
for(int i=0;i<10;i++) {
System.out.println(this.getName() + i);
if(i == 3) {
Thread.yield();
}
}
}
public static void main(String[] args) {
YieldThreadyield1 = new YieldThread("Yield-高优先级");
YieldThreadyield2 = new YieldThread("Yield-低优先级");
//设置优先级
yield1.setPriority(Thread.MAX_PRIORITY);
yield2.setPriority(Thread.MIN_PRIORITY);
//启动两个并发线程
yield1.start();
yield2.start();
}
}
原文:http://blog.csdn.net/w_basketboy24/article/details/37690433