程序、進程和線程
???? 程序式計算機指令的集合,它以文件的形式存儲在磁盤上
???? 進程:是一個程序在其自身的地址空間中的一次執行活動
???? 進程是資源申請、調度和獨立運行的單位,因此,它使用系統中的運行資源;而程序不能申請系統資源,不能被系統調度,也不能作為獨立運行的單位,因此,它不占有系統地運行資源
???? 線程:是進程中的一個單一的連續控制流程。一個進程可以擁有多個線程,但至少有一個線程
???? 線程又稱為輕量級進程,它和進程一樣擁有獨立的執行控制,由操作系統負責調度,區別在于線程沒有獨立的存儲空間,而是和所屬進程中的其他線程共享一個存儲空間,這使得線程間的通信遠較進程簡單
???? 單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--;
???????? }
?????? }
}
posted on 2007-08-18 14:02
jadmin 閱讀(76)
評論(0) 編輯 收藏