标签:
进程:每个进程都有独立的代码和数据空间(进程上下文),进程间的切换会有较大的开销,一个进程包含1--n个线程。
线程:同一类线程共享代码和数据空间,每个线程有独立的运行栈和程序计数器(PC),线程切换开销小。
线程和进程一样分为五个阶段:创建、就绪、运行、阻塞、终止。
多进程是指操作系统能同时运行多个任务(程序)。
多线程是指在同一程序中有多个顺序流在执行。
在java中要想实现多线程,有两种手段,一种是继承Thread类,另外一种是实现Runnable接口,为什么会有两种方式,原因是由于java具有单继承性质,当有些类已经实现继承,但需要多线程时,我们给它另外一种办法,实现Runnable接口
public class Thread implements Runnable
package cn.sun.thread;
public class MyThread extends Thread{
@Override
public void run() {
super.run();
System.out.println("this is my first Thread : MyThread");
}
}
package cn.sun.test;
import cn.sun.thread.MyThread;
public class ThreadTest01 {
public static void main(String[] args) {
MyThread mythread = new MyThread();
mythread.start();
System.out.println("program over");
}
}
program over
this is my first Thread : MyThread
package cn.sun.thread;
public class MyThread extends Thread{
@Override
public void run() {
try {
for(int i=0; i<10; i++){
int time = (int) (Math.random()*1000);
Thread.sleep(time);
System.out.println("run="+Thread.currentThread().getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
package cn.sun.thread;
public class MyThreadTest02 {
public static void main(String[] args) {
try {
MyThread thread = new MyThread();
thread.setName("myThread");
thread.start();
for(int i=0; i<10; i++){
int time = (int) (Math.random()*1000);
Thread.sleep(time);
System.out.println("run="+Thread.currentThread().getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
run main
run MyThread
run main
run main
run MyThread
run MyThread
run main
run MyThread
run main
run main
run MyThread
run main
run MyThread
run MyThread
run MyThread
run main
run main
run MyThread
run main
run MyThread
package cn.sun.runable;
public class MyRunnable implements Runnable{
@Override
public void run() {
System.out.println("MyRunnable run...");
}
}
package cn.sun.test;
import cn.sun.runable.MyRunnable;
public class MyRunnableTest01 {
public static void main(String[] args) {
Runnable runnable = new MyRunnable();
Thread thread = new Thread(runnable);
thread.start();
System.out.println("main over...");
}
}
main over...
MyRunnable run...
package cn.sun.safe;
public class MyThread extends Thread {
private int count = 5;
public MyThread(String name){
super();
this.setName(name);
}
@Override
public void run() {
super.run();
while(count > 0){
count--;
System.out.println(this.currentThread().getName()+"‘s count is :" +count);
}
}
}
package cn.sun.test;
import cn.sun.safe.MyThread;
public class ThreadTest02 {
public static void main(String[] args) {
MyThread threadA = new MyThread("A");
MyThread threadB = new MyThread("B");
MyThread threadC = new MyThread("C");
threadA.start();
threadB.start();
threadC.start();
}
}
A‘s count is :4
A‘s count is :3
A‘s count is :2
A‘s count is :1
A‘s count is :0
C‘s count is :4
C‘s count is :3
C‘s count is :2
B‘s count is :4
B‘s count is :3
B‘s count is :2
C‘s count is :1
C‘s count is :0
B‘s count is :1
B‘s count is :0
package cn.sun.safe;
public class MyThreadShare extends Thread {
private int count = 5;
@Override
public void run() {
super.run();
count--;
System.out.println(this.currentThread().getName()+"‘s count is :" +count);
}
}
package cn.sun.test;
import cn.sun.safe.MyThreadShare;
public class ThreadTest03 {
public static void main(String[] args) {
MyThreadShare myThread = new MyThreadShare();
Thread a = new Thread(myThread,"A");
Thread b = new Thread(myThread,"B");
Thread c = new Thread(myThread,"C");
Thread d = new Thread(myThread,"D");
Thread e = new Thread(myThread,"E");
a.start();
b.start();
c.start();
d.start();
e.start();
}
}
B‘s count is :3
C‘s count is :2
A‘s count is :3
D‘s count is :1
E‘s count is :0
package cn.sun.safe;
public class MyThreadShare extends Thread {
private int count = 5;
@Override
synchronized public void run() {
super.run();
count--;
System.out.println(this.currentThread().getName()+"‘s count is :" +count);
}
}
A‘s count is :4
C‘s count is :3
B‘s count is :2
E‘s count is :1
D‘s count is :0
标签:
原文地址:http://blog.csdn.net/sunfc_nbu/article/details/51133477