<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            java語言

            java多線程介紹

            時間:2025-05-28 15:22:17 java語言 我要投稿

            java多線程介紹

              多線程的基本實現

              進程指運行中的程序,每個進程都會分配一個內存空間,一個進程中存在多個線程,啟動一個JAVA虛擬機,就是打開個一個進程,一個進程有多個線程,當多個線程同時進行,就叫并發。

              Java創建線程的兩種方式為: 繼承Thread類 和實現Runnable接口

              Thread類

              1、通過覆蓋run方法實現線程要執行的程序代碼

              2、Start()開始執行多線程

              package com.bin.duoxiancheng;

              public class d1 extends Thread{

              public void run(){

              for(int i=0 ; i<50; i++){

              System.out.println(i);

              System.out.println(currentThread()。getName());

              try {

              sleep(100);

              } catch (InterruptedException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

              }

              }

              }

              public static void main(String[] args){

              new d1()。start();

              new d1()。start();

              }

              }

              多個線程共享一個實例的時候,代碼代碼如下:

              package com.bin.duoxiancheng;

              public class d1 extends Thread{

              int i=0;

              public void run(){

              for(i=0 ; i<50; i++){

              System.out.println(i);

              System.out.println(currentThread()。getName());

              try {

              sleep(100);

              } catch (InterruptedException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

              }

              }

              }

              public static void main(String[] args){

              new d1()。start();

              new d1()。start();

              }

              }

              結果如下所示:

              Thread-1

              Thread-0

              1

              Thread-1

              1

              實際2個線程在操縱不同的變量a,在執行run方法時候,線程把a都當做自己的變量在執行。

              Runnable接口實現多線程

              當一個繼承自Thread時,就不能再繼承其他類,使用Runnable接口解決了此問題,在新建一個Thread類中,在構造方法中初始化

              Thread(Runnable target)

              分配新的 Thread 對象。

              Thread(Runnable target,String name)

              分配新的 Thread 對象。

              package com.bin.duoxiancheng;

              public class D2 implements Runnable{

              int i=0;

              public void run(){

              for(i=0 ; i<50; i++){

              System.out.println(i);

              System.out.println(Thread.currentThread()。getName());

              try {

              Thread.sleep(100);

              } catch (InterruptedException e) {

              // TODO Auto-generatedcatch block

              e.printStackTrace();

              }

              }

              }

              public static void main(String[] args){

              D2 d=new D2();

              Thread t=new Thread(d);

              t.start();

              }

              }

            【java多線程介紹】相關文章:

            Java多線程的用法介紹09-15

            關于Java多線程介紹09-09

            java的多線程09-09

            java多線程08-31

            Java多線程知識點介紹09-05

            java語言的多線程08-29

            java多線程教程11-03

            如何使用java多線程08-23

            Java多線程問題總結10-24

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看