## Java 多线程 ##

目录:
【1】 多线程与多进程的概念。
【2】 多线程的建立方法。
【3】 多线程之间的数据共享,同步控制,通信。

【1】

一个CPU每次只能做一件事,通过操作系统对CPU等资源进行分配,在很短的时间里交替执行多个程序,给人同时执行的感觉,同时提高了资源利用率。
多进程相当于多个应用同时执行,比如说,QQ和office同时运行,而多进程则是相当于office中同时编辑多个文档,他们是隶属于同一个应用程序。多进程之间存在很多不方便使用的问题。第一个,设置一个进程会占用想到那个一部分处理器时间和内存资源(花费太大)。第二个,多进程之间大多数不允许访问其他进程的内存空间,难以通信。多线程刚好可以克服这些问题。同时一个进程至少拥有一个线程在执行其代码。什么时候应用多线程?想要实现一个程序中的多段代码并发执行的时候,可以考虑多线程。特别是当程序要处理一个很耗时间的代码,而其他代码并不依赖该段代码的结果时候。多线程也不能滥用,因为多线程之间的一致性维护等也需要耗费一定资源。
【2】
java中如何创建多线程?
java提供语言级别的线程支持,java中Thread(多线程类,继承Object类)在java.lang包中,所以可以直接使用。创建线程有两种方法:继承Thread类,实现Runnable接口。
继承 Thread类,重写他的 run()方法,再在需要调用该线程的地方调用start()方法。

    `class myThread extends Thread{
        private int num;
        public myThread(int newnum){
            this.num=newnum;
        }
        @Override
        public void run(){
            System.out.println("subThread started!");
            int time=num;
            int a=0,b=1;
            while(time>1){
                int t=b;
                b=a+b;
                a=t;
                time--;
            }
            System.out.println("The numth number is "+ b);
        }
    }

    public class ThreadTest {
        public static void main(String[] args) {
        System.out.println("Main thread starts!");
        myThread th=new myThread(7);
        th.start();
        System.out.println("Main thread ends!");
        }
    }`
输出结果:
Main thread starts!
Main thread ends!
subThread started!
The numth number is 13

注意,主程序在执行了start()方法之后,不等返回,直接跳到后面一句执行

实现Runnable()接口,在类已经继承某个类之后可以使用。(java不允许多继承)。
class myThread implements Runnable{
    private int num;
    public myThread(int newnum){
        this.num=newnum;
    }

    public void run(){
        System.out.println("subThread started!");
        int time=num;
        int a=0,b=1;
        while(time>1){
            int t=b;
            b=a+b;
            a=t;
            time--;
        }
        System.out.println("The numth number is "+ b);
    }
}

public class ThreadTest {

    public static void main(String[] args) {
        System.out.println("Main thread starts!");
        myThread th=new myThread(8);
        //th.start(); Runnable()//接口只有 run()方法,不能如此调用,//因此使用下面的匿名类似的方法使用
        new Thread(th).start();
        System.out.println("Main thread ends!");

    }

}

结果:

Main thread starts!
Main thread ends!
subThread started!
The numth number is 21

事实上,实现接口Runnable()可以更方便的解决多个线程之间处理共享资源的问题。
【3】
数据共享:
以一个Runnable类型的对象作为参数创建三个新线程,那么这三个线程共享数据。

class myTestThread implements Runnable{
    private int sleepTime;
    public myTestThread (){
        this.sleepTime=(int)(Math.random()*6000);
    }

    @Override
    public void run() {
        try{
            System.out.println(Thread.currentThread().getName()+"going to sleeping for "+this.sleepTime);
            Thread.sleep(this.sleepTime);
        }
        catch(InterruptedException exception ){

        }
        System.out.println(Thread.currentThread().getName()+" finished!");
    }



}

public class mythread1 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        myTestThread mt=new myTestThread();
        System.out.println("Main Thread begins!");
        new Thread(mt,"Thread1").start();
        new Thread(mt,"Thread2").start();
        new Thread(mt,"Thread3").start();
        System.out.println("Main Thread ends!");
    }

}

打印出的结果:

Main Thread begins!
Main Thread ends!
Thread2going to sleeping for 3754
Thread3going to sleeping for 3754
Thread1going to sleeping for 3754
Thread2 finished!
Thread3 finished!
Thread1 finished!

如果创建三个继承接口的对象,那么会有三个不同停止时间的线程。
为了同步线程,可以通过, synchronized 关键词。该关键相当于锁的功能,处理的是对象。

线程之间的通信。
主要方法:
public final void wait();//该线程暂停进入调用对象的等待池,并且释放已经获得的调用对象的旗标值(锁)。直到调用对象调用 notify()或者notifyAll()方法。
public void notify();//唤醒正在等待的第一个线程
public void notifyAll();//唤醒正在等待的所有的线程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值