Java笔记(六 程序、进程和线程)

来源:百度文库 编辑:神马文学网 时间:2024/04/20 09:50:03
ctrl+C可以强制停止Java程序的执行
程序、进程和线程
程序式计算机指令的集合,它以文件的形式存储在磁盘上
进程:是一个程序在其自身的地址空间中的一次执行活动
进程是资源申请、调度和独立运行的单位,因此,它使用系统中的运行资源;而程序不能申请系统资源,不能被系统调度,也不能作为独立运行的单位,因此,它不占有系统地运行资源
线程:是进程中的一个单一的连续控制流程。一个进程可以拥有多个线程,但至少有一个线程
线程又称为轻量级进程,它和进程一样拥有独立的执行控制,由操作系统负责调度,区别在于线程没有独立的存储空间,而是和所属进程中的其他线程共享一个存储空间,这使得线程间的通信远较进程简单
单CPU下某一个时刻只能有一个线程在运行
Java对多线程的支持
Java在语言级提供了对多线程程序设计的支持
实现多线程程序的两种方式:
(1)从Thread类(java.lang包)继承:A thread is a thread of execution in a program.
(2)实现Runnable接口
Java运行时系统实现了一个用于调度线程执行的线程调度器(其他的语言一般是由OS调度的),用于确定某一时刻有哪一个线程在CPU上运行
在Java技术中,线程通常是抢占式的而不需要时间片分配进程(分配给多个线程相等的CPU时间的进程)。抢占式调度模型就是许多线程处于可以运行状态(等待状态),但实际上只有一个线程在运行。该线程一只运行到它终止,进入可运行状态(等待状态),或者另一个具有更高优先级的线程变成可运行状态。在后一种情况下,低优先级的线程被高优先级的线程抢占,高优先级的线程获得运行的机会
Java线程调度器支持不同优先级线程的抢占方式,但其本身不支持相同优先级线程的时间片轮换
Java运行时系统所在的操作系统(例如:Windows2000)支持时间片的轮换,则线程调度器就支持相同优先级线程的时间片轮换
----------------------------------------------------------------------------------------------------
实现多线程程序的一种方式:从Thread类继承
class MultiThread
{
public static void main(String[] args)//main()方法也是在一个线程当中被执行的
{
MyThread mt=new MyThread();
//mt.setDaemon(true);//将mt声明为后台线程。public final void setDaemon boolean on):Marks this thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads.This method must be called before the thread is started.on - if true, marks this thread as a daemon thread.
mt.setPriority(Thread.MAX_PRIORITY);//设置线程优先级void setPriority(int newPriority):Changes the priority of this thread;static int MAX_PRIORITY:The maximum priority that a thread can have.
mt.start();//void start():Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
int index=0;
while(true)
{
if(index++==1000)
break;
System.out.println("main:"+Thread.currentThread().getName());//static Thread currentThread():Returns a reference to the currently executing thread object; String getName():Returns this thread‘s name.
}
}
}
class MyThread extends Thread//There are two ways to create a new thread of execution. One is to declare a class to be a subclass of Thread. This subclass should override the run method of class Thread.
{
public void run()
{
while(true)
{
System.out.println(getName());
//yield();//中止自己static void yield():Causes the currently executing thread object to temporarily pause and allow other threads to execute.
}
}
}
/*
D:\java\L5>javac MultiThread.java
D:\java\L5>java MultiThread
main:main
Thread-0
在main()里,原本写的是先调用mt.start(),即启动mt线程,再打印main线程,但结果里是先打印出来了main线程。这是因为OS分配给main线程的时间片刚开始还没有用完,所以继续执行打印了main线程,等main线程的时间片执行完了,才执行的MyThread线程
*/
-----------------------------------------------------------------------------------------------------------------
实现多线程程序的另一种方式:实现Runnable接口The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
class MultiThread
{
public static void main(String[] args)
{
MyThread mt=new MyThread();
new Thread(mt).start();//Thread的一个构造方法:Thread(Runnable target):Allocates a new Thread object.
int index=0;
while(true)
{
System.out.println("main:"+Thread.currentThread().getName());
}
}
}
class MyThread implements Runnable//这个MyThread已经不是从Thread类派生来的了
{
public void run()
{
while(true)
{
System.out.println(Thread.currentThread().getName());
}
}
}
------------------------------------------------------------------------------------------
线程的同步
The code segments within a program that access the same object from separate,conxurrent threads are called "critical sections"
同步的两种方式:同步快和同步方法。不管是那种方式,都是用synchronized来实现的
每一个对象都有一个监视器,或者叫做锁。同步方法利用的是this所代表的对象的锁。每个class也有一个锁,是这个class所对应的Class对象的锁
wait、notify、notifyAll
每一个对象出了一个锁之外,还有一个等待队列(wait set),当一个对象刚创建的时候,它的等待队列时空的
我们应该在当前线程锁住对象的锁后,去掉用该对象的wait方法
当调用对象的notify方法时,将从该对象的等待队列中删除一个任意选择的线程,这个线程将再次成为可运行的线程
当调用对象的notifyAll方法时,将从该对象的等待队列中删除所有等待的线程,这些线程将成为可运行的线程
wait和notify主要用于生产者—消费者这种关系中
-------------------------------------------------------------------------------------------
火车站售票系统
class TicketsSystem
{
public static void main(String[] args)
{
SellThread st=new SellThread();//同时卖这100张票,不是应该创建4个SellThread对象(SellThread st1=new SellThread()),因为如果是创建4个SellThread对象,那每个对象里都有100张票
new Thread(st).start();//创建4个线程同时卖这100张票
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)//可以用synchronized(this)
{
if(tickets>0)
{
try
{
Thread.sleep(10);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+
" sell ticket:"+tickets);
tickets--;
}
}
//sell();
}
}
public synchronized void sell()
{
if(tickets>0)
{
System.out.println(Thread.currentThread().getName()+
" sell ticket:"+tickets);
tickets--;
}
}