欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
Java筆記(六 程序、進(jìn)程和線(xiàn)程)

Java筆記(六 程序、進(jìn)程和線(xiàn)程)

  ctrl+C可以強制停止Java程序的執行
 
  程序、進(jìn)程和線(xiàn)程
    程序式計算機指令的集合,它以文件的形式存儲在磁盤(pán)上
    進(jìn)程:是一個(gè)程序在其自身的地址空間中的一次執行活動(dòng)
    進(jìn)程是資源申請、調度和獨立運行的單位,因此,它使用系統中的運行資源;而程序不能申請系統資源,不能被系統調度,也不能作為獨立運行的單位,因此,它不占有系統地運行資源
    線(xiàn)程:是進(jìn)程中的一個(gè)單一的連續控制流程。一個(gè)進(jìn)程可以擁有多個(gè)線(xiàn)程,但至少有一個(gè)線(xiàn)程
    線(xiàn)程又稱(chēng)為輕量級進(jìn)程,它和進(jìn)程一樣擁有獨立的執行控制,由操作系統負責調度,區別在于線(xiàn)程沒(méi)有獨立的存儲空間,而是和所屬進(jìn)程中的其他線(xiàn)程共享一個(gè)存儲空間,這使得線(xiàn)程間的通信遠較進(jìn)程簡(jiǎn)單
    單CPU下某一個(gè)時(shí)刻只能有一個(gè)線(xiàn)程在運行
 
  Java對多線(xiàn)程的支持
    Java在語(yǔ)言級提供了對多線(xiàn)程程序設計的支持
    實(shí)現多線(xiàn)程程序的兩種方式:
      (1)從Thread類(lèi)(java.lang包)繼承:A thread is a thread of execution in a program.
      (2)實(shí)現Runnable接口
    Java運行時(shí)系統實(shí)現了一個(gè)用于調度線(xiàn)程執行的線(xiàn)程調度器(其他的語(yǔ)言一般是由OS調度的),用于確定某一時(shí)刻有哪一個(gè)線(xiàn)程在CPU上運行
    在Java技術(shù)中,線(xiàn)程通常是搶占式的而不需要時(shí)間片分配進(jìn)程(分配給多個(gè)線(xiàn)程相等的CPU時(shí)間的進(jìn)程)。搶占式調度模型就是許多線(xiàn)程處于可以運行狀態(tài)(等待狀態(tài)),但實(shí)際上只有一個(gè)線(xiàn)程在運行。該線(xiàn)程一只運行到它終止,進(jìn)入可運行狀態(tài)(等待狀態(tài)),或者另一個(gè)具有更高優(yōu)先級的線(xiàn)程變成可運行狀態(tài)。在后一種情況下,低優(yōu)先級的線(xiàn)程被高優(yōu)先級的線(xiàn)程搶占,高優(yōu)先級的線(xiàn)程獲得運行的機會(huì )
    Java線(xiàn)程調度器支持不同優(yōu)先級線(xiàn)程的搶占方式,但其本身不支持相同優(yōu)先級線(xiàn)程的時(shí)間片輪換
    Java運行時(shí)系統所在的操作系統(例如:Windows2000)支持時(shí)間片的輪換,則線(xiàn)程調度器就支持相同優(yōu)先級線(xiàn)程的時(shí)間片輪換
----------------------------------------------------------------------------------------------------
實(shí)現多線(xiàn)程程序的一種方式:從Thread類(lèi)繼承
class MultiThread
{
       public static void main(String[] args)//main()方法也是在一個(gè)線(xiàn)程當中被執行的 
       {
             MyThread mt=new MyThread();
             //mt.setDaemon(true);//將mt聲明為后臺線(xiàn)程。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);//設置線(xiàn)程優(yōu)先級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ě)的是先調用mt.start(),即啟動(dòng)mt線(xiàn)程,再打印main線(xiàn)程,但結果里是先打印出來(lái)了main線(xiàn)程。這是因為OS分配給main線(xiàn)程的時(shí)間片剛開(kāi)始還沒(méi)有用完,所以繼續執行打印了main線(xiàn)程,等main線(xiàn)程的時(shí)間片執行完了,才執行的MyThread線(xiàn)程
*/
-----------------------------------------------------------------------------------------------------------------
實(shí)現多線(xiàn)程程序的另一種方式:實(shí)現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è)構造方法: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());
                  }
             } 
}

------------------------------------------------------------------------------------------
  線(xiàn)程的同步
    The code segments within a program that access the same object from separate,conxurrent threads are called "critical sections"
    同步的兩種方式:同步快和同步方法。不管是那種方式,都是用synchronized來(lái)實(shí)現的
    每一個(gè)對象都有一個(gè)監視器,或者叫做鎖。同步方法利用的是this所代表的對象的鎖。每個(gè)class也有一個(gè)鎖,是這個(gè)class所對應的Class對象的鎖
 
  wait、notify、notifyAll
    每一個(gè)對象出了一個(gè)鎖之外,還有一個(gè)等待隊列(wait set),當一個(gè)對象剛創(chuàng )建的時(shí)候,它的等待隊列時(shí)空的
    我們應該在當前線(xiàn)程鎖住對象的鎖后,去掉用該對象的wait方法
    當調用對象的notify方法時(shí),將從該對象的等待隊列中刪除一個(gè)任意選擇的線(xiàn)程,這個(gè)線(xiàn)程將再次成為可運行的線(xiàn)程
    當調用對象的notifyAll方法時(shí),將從該對象的等待隊列中刪除所有等待的線(xiàn)程,這些線(xiàn)程將成為可運行的線(xiàn)程
    wait和notify主要用于生產(chǎn)者—消費者這種關(guān)系中
-------------------------------------------------------------------------------------------
火車(chē)站售票系統
class TicketsSystem
{
           public static void main(String[] args)
           {
               SellThread st=new SellThread();//同時(shí)賣(mài)這100張票,不是應該創(chuàng )建4個(gè)SellThread對象(SellThread st1=new SellThread()),因為如果是創(chuàng )建4個(gè)SellThread對象,那每個(gè)對象里都有100張票
               new Thread(st).start();//創(chuàng )建4個(gè)線(xiàn)程同時(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--; 
        }
      } 
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
java中,如何安全的結束一個(gè)正在運行的線(xiàn)程?
Java總結篇系列:Java多線(xiàn)程(一)
初學(xué)Java多線(xiàn)程:用Thread類(lèi)創(chuàng )建線(xiàn)程
java并發(fā)(四)如何創(chuàng )建并運行java線(xiàn)程
Java線(xiàn)程(四):線(xiàn)程中斷、線(xiàn)程讓步、線(xiàn)程睡眠、線(xiàn)程合并
Thread詳解2:停止與中斷
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久