程序、进程和线程

1.程序是计算机指令的集合,它以文件的形式存储在磁盘上。

2.进程:是一个程序在其自身的地址空间中的一次执行活动。

3.进程是资源申请、调度和独立运行的单位,因此,它使用系统中的运行资源;
而程序不能申请系统资源,不能被系统调度,也不能作为独立运行的单位,因此,它不占用系统的运行资源。

4.线程:是进程中的一个单一的连续控制流程。一个进程可以拥有多个线程。

5.线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是和所属进程中的其它线程共享一个存储空间,这使得线程间的通信远较进程简单。


Java对多线程的支持

Java在语言级提供了对多线程程序设计的支持。

实现多线程程序的两种方式:   
(1)从Thread类继承; 
(2)实现Runnable接口。


Java对多线程的支持

1.Java运行时系统实现了一个用于调度线程执行的线程调度器,
用于确定某一时刻由哪一个线程在CPU上运行。

2.在java技术中,线程通常是抢占式的而不需要时间片分配进程(分配给每个线程相等的CPU时间的进程)。抢占式调度模型就是许多线程处于可以运行状态(等待状态),但实际上只有一个线程在运行。该线程一直运行到它终止进入可运行状态(等待状态),或者另一个具有更高优先级的线程变成可运行状态。在后一种情况下,低优先级的线程被高优先级的线程抢占,高优先级的线程获得运行的机会。 

3.Java线程调度器支持不同优先级线程的抢先方式,但其本身不支持相同优先级线程的时间片轮换。

4.Java运行时系统所在的操作系统(例如:Windows2000)支持时间片的轮换,则线程调度器就支持相同优先级线程的时间片轮换。


线程的同步

The code segments within a program that access the same object from separate, concurrent threads are called “critical sections”。

同步的两种方式:同步块和同步方法每一个对象都有一个监视器,或者叫做锁。

同步方法利用的是this所代表的对象的锁。

每个class也有一个锁,是这个class所对应的Class对象的锁。




线程的死锁

哲学家进餐的问题

线程1锁住了对象A的监视器,等待对象B的监视器,线程2锁住了对象B的监视器,等待对象A的监视器,就造成了死锁。



wait、notify、notifyAll

每一个对象除了有一个锁之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的对待队列是空的。

我们应该在当前线程锁住对象的锁后,去调用该对象的wait方法。

当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程。

当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程。

wait和notify主要用于producer-consumer这种关系中。


线程的终止

设置一个flag变量。

结合interrupt()方法。


//进程测试例子

class MutiThread{
public static void main(String[] args){
MyThread mt=new MyThread();
/*new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();
new Thread(mt).start();*/
mt.getThread().start();
mt.getThread().start();
mt.getThread().start();
mt.getThread().start();
//mt.setDaemon(true);
//mt.setPriority(Thread.MAX_PRIORITY);
//mt.start();
int index=0;
while(true){
/*if(index++ == 1000)
break;*/
System.out.println(Thread.currentThread().getName());
}

}
}

class MyThread /*implements Runnableextends Thread*/{
int index=0;
private class InnerThread extends Thread{
public void run() {
while(true){
System.out.println(Thread.currentThread().getName()+":"+index++);
}
}
}
Thread getThread(){
return new InnerThread();
}
/*public void run(){
while(true){
System.out.println(Thread.currentThread().getName()+":"+index++);
//yield();
}

}*/
}



//售票系统

public class TicketsSystem {

public static void main (String[] args) {
SellThread st=new SellThread();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
new Thread(st).start();
}


}

class SellThread implements Runnable {
int tickets=100;
Object obj=new Object();
public void run(){
while(true){
synchronized(obj){
if(tickets>0){
try{
Thread.sleep(10);
}catch(Exception e){
e.printStackTrace();
}
synchronized(this){

System.out.println(Thread.currentThread().getName()+" " +tickets);
tickets--;
}
}
}
sell();
}
}
public synchronized void sell(){
synchronized(obj){
if(tickets>0){
try{
Thread.sleep(1000);
}catch(Exception e){
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" " +tickets);
tickets--;
}
}
}
}



//producer-consumer 测试

/**
* @(#)Test.java
*
*
* @author
* @version 1.00 2012/5/4
*/


public class Test {
public static void main (String[] args) {
Queue q=new Queue();
Producer p=new Producer(q);
Consumer c=new Consumer(q);
p.start();
c.start();
}
}

class Producer extends Thread{
Queue q;
Producer(Queue q){
this.q=q;
}
public void run(){
for(int i=0;i<10;i++){
q.put(i);
System.out.println("Producer put "+i);
}
}
}


class Consumer extends Thread{
Queue q;
Consumer(Queue q){
this.q=q;
}
public void run(){
while(true){
System.out.println("Consumer get "+q.get());
}
}
}

class Queue{
int value;
boolean bFull=false;
public synchronized void put(int i){
if(!bFull){
value = i;
bFull=true;
notify();
}
try{
wait();
}catch(Exception e){
e.printStackTrace();
}
}
public synchronized int get(){
if(!bFull){
try{
wait();
}catch(Exception e){
e.printStackTrace();
};
}
bFull=false;
notify();
return value;
}
}