欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

新手了解java 多线程基础知识(一)

程序员文章站 2022-06-17 19:52:54
目录3.thread类方法介绍1.基本概念程序、进程、线程 程序(program)是为完成特定任务、用某种语言编写的一组指令的集 合。即指一段静态的代码,静态对象。 进程(process)...

1.基本概念

程序、进程、线程

  • 程序(program)是为完成特定任务、用某种语言编写的一组指令的集 合。即指一段静态的代码,静态对象。
  • 进程(process)是程序的一次执行过程,或是正在运行的一个程序。是 一个动态的过程:有它自身的产生、存在和消亡的过程——具有生命 周期。可以理解为一个正在运行的软件。
  • 线程(thread),进程可进一步细化为线程,是一个程序内部的一条执行 路径。可以理解为一个软件的功能。

多线程程序的优点:

  • 提高应用程序的响应。对图形化界面更有意义,可增强用户体验。
  • 提高计算机系统cpu的利用率。
  • 改善程序结构。将既长又复杂的进程分为多个线程,独立运行,利于理 解和修改。

2.多线程的创建

​ 在java中我们可以使用java.lang.thread类来实现 ,要想我们的类具有多线程的功能,需要让我们的类去继承thread类,然后重写run()方法,并调用 start()方法来启动多线程。

示例 1:

public class mythread extends thread{
    public void run(){
        for (int i = 0; i < 50; i++) {
            system.out.println("mythread:"+i);
        }
    }
}
public class mythreadtest{
    public static void main(string[] args){
        thread t1 = new mythread();
        t1.start();
        for (int i = 0; i < 20; i++) {
            system.out.println("world=====" + i);
        }
    }
}

说明:创建一个java类继承thread类,并重写父类thread中的run方法,在run方法中写具体的多线程业务。创建线程类的对象,并调用start方法启动多线程。

**注意:**多线程的启动调用的是start方法,在jvm底层中start方法内部会调用run方法。

一个对象只需要调用一次start()方法,如果多次调用则会抛出异常“illegalthreadstateexception”。

示例 2:

public class myrunnable implements runnable {
    public void run() {
        for (int i = 0; i < 50 ; i++) {
            system.out.println( "myrunnable:"+i);
        }
    }
}
public class threaddemo {
    public static void main(string[] args) {
        thread thread = new thread( new myrunnable());
        thread.start();// 只有调用thread类中的start()方法才可以实现多线程
            for (int i = 0; i < 50; i++) {
                system.out.println("main:" + i);
            }
    }
}

说明:

  • 编写一个类,实现runnable接口;
  • 重写run()方法;
  • 根据runnable子类对象来创建thread对象;
  • 通过thread对象调用start()方法来启动多线程;

总结:

  • 继承thread:重写run()方法,业务代码在run()中。
  • 实现runnable:线程代码存在接口的子类的run方法。
  • 通过callable和线程池的方式创建线程。

**提示:**在应用中我们如果可以使用runable接口那么就尽量使用,这样可以避免java单继承的局限。

3.thread类方法介绍

1)currentthread():返回当前正在执行的线程对象的引用。

2)getname():返回当前线程的名称

3)isalive():判断当前线程是否存活

4)isdeaomon():判断线程是否为守护线程

5)join():在当前线程中引入另一个线程,而当前线程会被阻塞

6)sleep():让当前线程进入睡眠状态

7)yield():让当前线程放弃cpu的执行权,重新进入排队,与其他线程平等争夺cpu执行。

8)interrupt() 中断线程 9)interrupted() 如果当前线程已经中断,则返回 true;否则返回 false。

示例:

public class threadtest {
    public static void main(string[] args) {
        //创建线程并开启线程1
        thread thread = new mythread();
        thread.start();
        //创建线程并开启线程2
        thread thread1 = new thread(new myrunnable());
        thread1.start();
        //创建线程并开启线程3
        mycallable mycallable = new mycallable();
        futuretask futuretask = new futuretask(mycallable);
        new thread(futuretask).start();

        for (int i = 0; i < 100; i++) {
            if (i== 50){
                //在main线程中当i=50加如thread线程,会在thread执行完后才会继续执行main线程剩下的
                try {
                    thread.join();
                } catch (interruptedexception e) {
                    e.printstacktrace();
                }
            }
            system.out.println("main:"+i);
        }
    }
}

public class mythread extends thread{
    public void run(){
        for (int i = 0; i < 100; i++) {
            // void interrupt() 中断线程 可以理解为线程中断状态有 true | false,每一次调用就是修改状态为true
            //static boolean interrupted() 如果当前线程已经中断,则返回 true;否则返回 false。
            thread.currentthread().interrupt();
            if (thread.interrupted()){
                system.out.println("thread interrupted");
            }
            // static currentthread()  返回对当前正在执行的线程对象的引用
            //string getname()   返回该线程的名称。
            //thread.currentthread().getname()  获取当前线程对象的名称
            system.out.println(thread.currentthread().getname()+":"+i);
        }
    }
}

public class myrunnable implements runnable{
    @override
    public void run() {
        for (int i = 0; i < 100; i++) {
            system.out.println("myrunnable"+i);
            //每次执行完打印让线程休眠1秒
            try {
                thread.sleep(1000);
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
        }
    }
}
public class mycallable implements callable {
    @override
    public object call() throws exception {
        for (int i = 0; i < 100; i++) {
            system.out.println("mycallable:"+i);
        }
        return null;
    }
}

总结

本篇文章就到这里了,希望对你有所帮助,也希望你能够多多关注的更多内容!