黑马程序员---java多线程初步

------- android培训java培训、期待与您交流! ----------

 

 

(一)多线程的原理:

在现代的操作系统中,进程是系统分配内存的基本单位,而线程是cpu执行的基本单位。一个应用程序可以有多个进程(但至少有一个进程),即拥有独立的内存空间用来执行相应的程序;一个进程可以有多个线程(但至少有一个线程,称为主线程),多个线程之间共享内存。

一个线程就是一个执行流程,多线程就是有多个并行的执行流程。多线程给人的感觉就是cpu在 同时 执行多个任务,但实际上一个cpu在同一时刻只能执行一个线程,只不过cpu在消耗完一个线程的时间片后,就回去立刻执行另一个线程 (cpu实际上还是一个线程一个线程的来执行)。但由于时间片非常短,线程间的切换非常快,所以给人的感觉就是多个线程在同时执行。

在操作系统中线程常见的三种状态为:就绪、运行、阻塞。只有处于运行状态的线程才占用cpu。

 


(二)线程的创建与启动:

创建线程的方法有两种:1.Thread t=new 继承Thread的子类()  2.实现Runnable接口即:Thread t=new Thread(new 实现Runnable接口的子类)。常用第二种方法来创建线程。

启动线程:t.start();执行完后即启动了相应的线程。线程启动完后会 自动 调用run()方法来执行线程的代码,run()方法执行完后,线程也就执行结束。
          线程何时执行,执行次序,是否中断等不受程序控制,所以线程的执行具有不稳定性。

 

继承Thread类创建线程,代码如下:

class hello extends Thread { 
  
    public hello() { 
  
    } 
  
    public hello(String name) { 
        this.name = name; 
    } 
  
    public void run() { 
        for (int i = 0; i < 5; i++) { 
            System.out.println(name + "运行     " + i); 
        } 
    } 
  
 public static void main(String[] args) { 
        hello h1=new hello("A"); 
        hello h2=new hello("B"); 
        h1.start(); 
        h2.start(); 
    } 
 
  
    private String name; 
} 

然后运行程序,输出的可能的结果如下:
A运行     0
B运行     0
B运行     1
B运行     2
B运行     3
B运行     4
A运行     1
A运行     2
A运行     3
A运行     4



因为需要用到CPU的资源,所以每次的运行结果基本是都不一样的,呵呵。

 

 

 

 


实现Runable接口创建线程,代码如下:

class hello implements Runnable { 
  
    public hello() { 
  
    } 
  
    public hello(String name) { 
        this.name = name; 
    } 
  
    public void run() { 

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

            System.out.println(name + "运行     " + i); 
        } 
    } 
  
    public static void main(String[] args) { 

        hello h1=new hello("线程A"); 

        Thread demo= new Thread(h1); 

        hello h2=new hello("线程B"); 

        Thread demo1=new Thread(h2); 

        demo.start(); 

        demo1.start(); 
    } 
  
    private String name; 
} 

【可能的运行结果】:
线程A运行      0
线程B运行     0
线程B运行     1
线程B运行     2
线程B运行     3
线程B运行     4
线程A运行      1
线程A运行      2
线程A运行      3
线程A运行      4


 

(三)Thread和Runnable的区别:
如果一个类继承Thread,则不适合资源共享。但是如果实现了Runable接口的话,则很容易的实现资源共享。

 

 

类继承Thread代码:

class hello extends Thread {
    public void run() {
        for (int i = 0; i < 7; i++) {
            if (count > 0) {
                System.out.println("count= " + count--);
            }
        }
    }
 
    public static void main(String[] args) {
        hello h1 = new hello();
        hello h2 = new hello();
        hello h3 = new hello();
        h1.start();
        h2.start();
        h3.start();
    }   
    private int count = 5;
}

【运行结果】:
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1
count= 5
count= 4
count= 3
count= 2
count= 1

 

 

我们换为Runnable接口:

class hello implements Runnable {
    public void run() {
        for (int i = 0; i < 7; i++) {
            if (count > 0) {
                System.out.println("count= " + count--);
            }
        }
    }
 
    public static void main(String[] args) {
        hello he=new hello();
        new Thread(he).start();
    }
 
    private int count = 5;
}

【运行结果】:
count= 5
count= 4
count= 3
count= 2
count= 1
 

总结:

实现Runnable接口比继承Thread类所具有的优势:

1):适合多个相同的程序代码的线程去处理同一个资源

2):可以避免java中的单继承的限制

3):增加程序的健壮性,代码可以被多个线程共享,代码和数据独立。

 

 

(四)线程的执行具有不稳定性:

class hello implements Runnable {
    public void run() {
        for (int i = 0; i < 3; i++) {
            System.out.println(Thread.currentThread().getName());
        }
    }
 
    public static void main(String[] args) {
        hello he = new hello();
        new Thread(he,"A").start();
        new Thread(he,"B").start();
        new Thread(he).start();
    }
}
【运行结果】:
A
A
A
B
B
B
Thread-0
Thread-0
Thread-0

说明如果我们没有指定名字的话,系统自动提供名字。

main方法其实也是一个线程。在java中所以的线程都是同时启动的,至于什么时候,哪个先执行,完全看谁先得到CPU的资源。

 

 

 

------- android培训java培训、期待与您交流! ----------  详细请查看:http://edu.csdn.net/heima/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值