程序、進(jìn)程和線程
???? 程序式計(jì)算機(jī)指令的集合,它以文件的形式存儲(chǔ)在磁盤(pán)上
???? 進(jìn)程:是一個(gè)程序在其自身的地址空間中的一次執(zhí)行活動(dòng)
???? 進(jìn)程是資源申請(qǐng)、調(diào)度和獨(dú)立運(yùn)行的單位,因此,它使用系統(tǒng)中的運(yùn)行資源;而程序不能申請(qǐng)系統(tǒng)資源,不能被系統(tǒng)調(diào)度,也不能作為獨(dú)立運(yùn)行的單位,因此,它不占有系統(tǒng)地運(yùn)行資源
???? 線程:是進(jìn)程中的一個(gè)單一的連續(xù)控制流程。一個(gè)進(jìn)程可以擁有多個(gè)線程,但至少有一個(gè)線程
???? 線程又稱(chēng)為輕量級(jí)進(jìn)程,它和進(jìn)程一樣擁有獨(dú)立的執(zhí)行控制,由操作系統(tǒng)負(fù)責(zé)調(diào)度,區(qū)別在于線程沒(méi)有獨(dú)立的存儲(chǔ)空間,而是和所屬進(jìn)程中的其他線程共享一個(gè)存儲(chǔ)空間,這使得線程間的通信遠(yuǎn)較進(jìn)程簡(jiǎn)單
???? 單CPU下某一個(gè)時(shí)刻只能有一個(gè)線程在運(yùn)行
?? Java對(duì)多線程的支持
???? Java在語(yǔ)言級(jí)提供了對(duì)多線程程序設(shè)計(jì)的支持
???? 實(shí)現(xiàn)多線程程序的兩種方式:
?????? (1)從Thread類(lèi)(java.lang包)繼承:A thread is a thread of execution in a program.
?????? (2)實(shí)現(xiàn)Runnable接口
???? Java運(yùn)行時(shí)系統(tǒng)實(shí)現(xiàn)了一個(gè)用于調(diào)度線程執(zhí)行的線程調(diào)度器(其他的語(yǔ)言一般是由OS調(diào)度的),用于確定某一時(shí)刻有哪一個(gè)線程在CPU上運(yùn)行
???? 在Java技術(shù)中,線程通常是搶占式的而不需要時(shí)間片分配進(jìn)程(分配給多個(gè)線程相等的CPU時(shí)間的進(jìn)程)。搶占式調(diào)度模型就是許多線程處于可以運(yùn)行狀態(tài)(等待狀態(tài)),但實(shí)際上只有一個(gè)線程在運(yùn)行。該線程一只運(yùn)行到它終止,進(jìn)入可運(yùn)行狀態(tài)(等待狀態(tài)),或者另一個(gè)具有更高優(yōu)先級(jí)的線程變成可運(yùn)行狀態(tài)。在后一種情況下,低優(yōu)先級(jí)的線程被高優(yōu)先級(jí)的線程搶占,高優(yōu)先級(jí)的線程獲得運(yùn)行的機(jī)會(huì)
???? Java線程調(diào)度器支持不同優(yōu)先級(jí)線程的搶占方式,但其本身不支持相同優(yōu)先級(jí)線程的時(shí)間片輪換
???? Java運(yùn)行時(shí)系統(tǒng)所在的操作系統(tǒng)(例如:Windows2000)支持時(shí)間片的輪換,則線程調(diào)度器就支持相同優(yōu)先級(jí)線程的時(shí)間片輪換
----------------------------------------------------------------------------------------------------
實(shí)現(xiàn)多線程程序的一種方式:從Thread類(lèi)繼承
class MultiThread
{
??????? public static void main(String[] args)//main()方法也是在一個(gè)線程當(dāng)中被執(zhí)行的
??????? {
????????????? MyThread mt=new MyThread();
????????????? //mt.setDaemon(true);//將mt聲明為后臺(tái)線程。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);//設(shè)置線程優(yōu)先級(jí)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()里,原本寫(xiě)的是先調(diào)用mt.start(),即啟動(dòng)mt線程,再打印main線程,但結(jié)果里是先打印出來(lái)了main線程。這是因?yàn)镺S分配給main線程的時(shí)間片剛開(kāi)始還沒(méi)有用完,所以繼續(xù)執(zhí)行打印了main線程,等main線程的時(shí)間片執(zhí)行完了,才執(zhí)行的MyThread線程
*/
-----------------------------------------------------------------------------------------------------------------
實(shí)現(xiàn)多線程程序的另一種方式:實(shí)現(xiàn)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的一個(gè)構(gòu)造方法:Thread(Runnable target):Allocates a new Thread object.
????????????? int index=0;
????????????? while(true)
????????????? {
????????????????? System.out.println("main:"+Thread.currentThread().getName());
????????????? }
??????? }
}
class MyThread implements Runnable//這個(gè)MyThread已經(jīng)不是從Thread類(lèi)派生來(lái)的了
{
????????????? 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來(lái)實(shí)現(xiàn)的
???? 每一個(gè)對(duì)象都有一個(gè)監(jiān)視器,或者叫做鎖。同步方法利用的是this所代表的對(duì)象的鎖。每個(gè)class也有一個(gè)鎖,是這個(gè)class所對(duì)應(yīng)的Class對(duì)象的鎖
?? wait、notify、notifyAll
???? 每一個(gè)對(duì)象出了一個(gè)鎖之外,還有一個(gè)等待隊(duì)列(wait set),當(dāng)一個(gè)對(duì)象剛創(chuàng)建的時(shí)候,它的等待隊(duì)列時(shí)空的
???? 我們應(yīng)該在當(dāng)前線程鎖住對(duì)象的鎖后,去掉用該對(duì)象的wait方法
???? 當(dāng)調(diào)用對(duì)象的notify方法時(shí),將從該對(duì)象的等待隊(duì)列中刪除一個(gè)任意選擇的線程,這個(gè)線程將再次成為可運(yùn)行的線程
???? 當(dāng)調(diào)用對(duì)象的notifyAll方法時(shí),將從該對(duì)象的等待隊(duì)列中刪除所有等待的線程,這些線程將成為可運(yùn)行的線程
???? wait和notify主要用于生產(chǎn)者—消費(fèi)者這種關(guān)系中
-------------------------------------------------------------------------------------------
火車(chē)站售票系統(tǒng)
class TicketsSystem
{
??????????? public static void main(String[] args)
??????????? {
??????????????? SellThread st=new SellThread();//同時(shí)賣(mài)這100張票,不是應(yīng)該創(chuàng)建4個(gè)SellThread對(duì)象(SellThread st1=new SellThread()),因?yàn)槿绻莿?chuàng)建4個(gè)SellThread對(duì)象,那每個(gè)對(duì)象里都有100張票
??????????????? new Thread(st).start();//創(chuàng)建4個(gè)線程同時(shí)賣(mài)這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--;
???????? }
?????? }
}
posted on 2007-08-18 14:02
jadmin 閱讀(76)
評(píng)論(0) 編輯 收藏