多线程的实现三种方式

多线程的实现三种方式:
1 继承thread类,重写run方法
继承thread方法就可以i调用thread类的start方法,,start方法调用java natvie start0();
这个是调用操作系统的方法,start方法

package com.cxy;

class Mythread01 extends  Thread{
    @Override
    public void run(){
        System.out.println("iii");
    }}
public class Mythread {
    public static void main(String[] args) {
        Mythread01 my = new Mythread01();
        my.start();
    }

}

2 实现runnable接口
接口实现是没有start方法,但是多线程必须采用start方法,所以需要调用new thread(mythread)
再调用start方法.

package com.cxy;

import sun.security.krb5.internal.Ticket;

class MyThread implements  Runnable{
    private Integer ticket=10;
    @Override
    public void  run() {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                System.out.println("zhgee" + i);
                System.out.println(ticket);
                ticket--;
                if (ticket<0){
                    break;
                }
            }
        }
    }
}
public class RunnableDemo {
    public static void main(String[] args) {
        MyThread myThread=new MyThread();
        new Thread(myThread).start();
        new Thread(myThread).start();
    }
}

前两中方法的区别:
thread类是runnable的接口子类,可以有效避免单继承的局限性
runnable接口可以很好的体现数据共享概念
如果继承thread类,那么就可以继承sart方法,runnable接口中,需要构建thread对象调用thread的start方法

3 实现callable接口

package com.cxy;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

class CallableThread implements Callable<String> {
    private Integer ticket = 10;

    @Override
    public String call() throws Exception {
        synchronized (this) {
            for (int i = 0; i < 10; i++) {
                System.out.println("zhgee" + i);
                System.out.println(ticket);
                ticket--;
                if (ticket < 0) {
                    break;
                }
            }
            return "票卖完了";
        }

    }
}
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableThread callableThread=new CallableThread();
        CallableThread callableThread1=new CallableThread();
        FutureTask<String> futureTask=new FutureTask(callableThread);
        FutureTask<String> futureTask1=new FutureTask(callableThread1);
        new Thread(futureTask).start();
        new Thread(futureTask1).start();
        System.out.println(futureTask.get());
        System.out.println(futureTask1.get());

    }

}

三个方法都必须调用start方法才可以启动线程

    public synchronized void start() {
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group‘s list of threads
         * and the group‘s unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
            start0();
            started = true;
        } finally {
            try {
                if (!started) {
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();
private native void start0();这个方法是用native修饰,表示调用操作系统的本地方法,所以就需要调用操作系统的方法

原文地址:https://www.cnblogs.com/xiufengchen/p/10655846.html

时间: 2024-07-29 19:58:50

多线程的实现三种方式的相关文章

java多线程开启的三种方式

1.继承Thread类,新建一个当前类对象,并且运行其start()方法 1 package com.xiaostudy.thread; 2 3 /** 4 * @desc 第一种开启线程的方式 5 * @author xiaostudy 6 * 7 */ 8 public class Demo1_Thread extends Thread { 9 10 public void run() { 11 for (int i = 0; i < 10; i++) { 12 System.out.pri

【java多线程】多线程的创建三种方式--笔记

申明:线程的概念以及进程的相关概念,可以参考网络上其他资料,这里只讨论多线程是怎么实现. 一.多线程的简单理解 明白什么是多线程,小生通俗一点的理解为:在一个程序里,我想同时让这个程序完成多个任务. 比如:让主函数 main 在打印1~100之间的所有整数的时候,要求在主函数打印到 20 的时候,再运行另一个类里的程序,让它打印10~100之间的所有整数. 这里忽略同进程内的多线程之间的抢占时间问题,上面的举例需求是要求一个程序只要有发生同时运行俩个程序的情况就行,即不准出现无论程序跑多少次都是

Java中 实现多线程成的三种方式(继承,实现,匿名内部类)

------------------------------------------------------------------------------------------------------------ /** 第一种方式:继承Thread类 *             1. 定义一个类,然后让该类继承Thread类 *             2. 重写run方法 *             3. 创建定义的这个类的对象 *             4. 启动线程 */ //继承

AJPFX关于JAVA多线程实现的三种方式

JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程.其中前两种方式线程执行完后都没有返回值,只有最后一种是带返回值的. 1.继承Thread类实现多线程继承Thread类的方法尽管被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,并且,启动线程的唯一方法就是通过Thread类的start()实例方法.start()方法

JAVA多线程实现的三种方式

JAVA多线程实现方式主要有三种:继承Thread类.实现Runnable接口.使用ExecutorService.Callable.Future实现有返回结果的多线程. 当中前两种方式线程运行完后都没有返回值,仅仅有最后一种是带返回值的. 1.继承Thread类实现多线程 继承Thread类的方法虽然被我列为一种多线程实现方式,但Thread本质上也是实现了Runnable接口的一个实例,它代表一个线程的实例,而且,启动线程的唯一方法就是通过Thread类的start()实例方法.start(

多线程开启的三种方式

class Program { static int Test(int i) { Console.WriteLine("test"+i); return 100; Thread.Sleep(10); //让当前线程休眠(暂停当前线程的执行)单位为毫秒ms } static void Main(string[] args) //在main方法中执行 一个线程的执行是从上往下执行的 { //通过委托来开启一个线程 //Func<int,int> a = Test; //IAsy

49、多线程创建的三种方式之继承Thread类

继承Thread类创建线程 在java里面,开发者可以创建线程,这样在程序执行过程中,如果CPU空闲了,就会执行线程中的内容. 使用Thread创建线程的步骤: 1.自定义一个类,继承java.lang包下的Thread类 2.重写run方法 3.将要在线程中执行的代码编写在run方法中 4.创建上面自定义类的对象 5.调用start方法启动线程 package com.sutaoyu.Thread; //1.自定义一个类,继承java.lang包下的Thread类 class MyThread

线程的状态以及创建多线程的三种方式

首先了解一下线程的五种状态: 新建状态: 新建状态是指new之后,即新创建了一个线程的时候,此时并未运行任何线程方法体内的程序代码. 就绪状态: 简单来说就是指程序调用了start()之后,线程就得到了启动,代表线程进入了就绪状态,但是此时并不代表它会立刻去执行run()方法体内的程序代码,而是随时等待cpu的调度. 运行状态: 获得cpu的时间后,调用run()方法,进入运行状态. 阻塞状态: 由于某种原因放弃了cpu的会用权力,暂时停止运行,等待再次被调用. 死亡状态: 线程正常执行完毕,或

IOS 多线程,线程同步的三种方式

一般情况下我们使用线程,在多个线程共同访问同一块资源.为保护线程资源的安全和线程访问的正确性. 在IOS中我们一般情况下使用以下三种线程同步代码方式: 第一种和第二种代码同步的使用方法,一般情况下我们只需要使用NSLock和NSCondition申明2个属性.然后给此属性赋对应的值.那么即可作为安全防控的线程手段. 同时也可以保证线程的资源安全. 1:NSLock方式 [xxxlock   lock] //上锁 同步代码块 [xxxlock   unlock]//解锁 2:NSCondition