多线程

作者: 嘉美伯爵 | 来源:发表于2017-05-15 17:00 被阅读9次

Thread类(须有start调用run才可启动线程)

public void start()
使该线程开始执行;Java 虚拟机调用该线程的 run 方法。
结果是两个线程并发地运行;当前线程(从调用返回给 start 方法)和另一个线程(执行其 run 方法)。多次启动一个线程是非法的。特别是当线程已经结束执行后,不能再重新启动。


class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        m1.run();
        m2.run();
        m3.run();
    }
}

多线程执行

class MyThread extends Thread{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        m1.start();
        m2.start();
        m3.start();
    }
}

Runnable接口(由于单继承局限)

public class Thread extends Object implements Runnable

class MyThread implements Runnable{
    private String name;
    public MyThread(String name){
        this.name = name;
    }
    public void run(){
        for(int x=0;x<200;x++){
            System.out.println(name+"----->"+x);
        }
    }
}
public class Demo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MyThread m1 = new MyThread("Java");
        MyThread m2 = new MyThread("Javb");
        MyThread m3 = new MyThread("Javc");
        new Thread(m1).start();
        new Thread(m2).start();
        new Thread(m3).start();
    }
}

用Runnable实现主体类,用Thread启动线程

Callable接口

暂无

线程的命名与取得

构造方法:Thread(Runnable target, String name)
public static Thread currentThread()此方法取得当前对象

package Demo;
//如果启动前未设置名字,则会自动编号命名
class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt).start();
        new Thread(mt).start();
        new Thread(mt).start();
    }
}
------------------------------------
Thread-0
Thread-1
Thread-2

设置名字:public final void setName(String name)

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"线程A").start();
        new Thread(mt).start();
        new Thread(mt,"线程B").start();
        new Thread(mt).start();
    }

}

取得名字:public final String getName()

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        System.out.println(Thread.currentThread().getName());
    }
}
public class CollableDemo {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"线程A").start();
        mt.run();
    }

}
--------------------------
main
线程A

主方法就是一个线程(main线程),在子方法上创建的线程为子线程;main只是进程上的一个子线程。

线程的休眠

public static void sleep(long millis,int nanos) throws InterruptedException

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<1000;x++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"线程A").start();
    }
}

如果设置了多个线程对象,则一起进入run()方法

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<1000;x++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        new Thread(mt,"线程A").start();
        new Thread(mt,"线程B").start();
        new Thread(mt,"线程C").start();
        new Thread(mt,"线程D").start();
        new Thread(mt,"线程E").start();
    }
}
------------------------------------
线程B,x =0
线程A,x =0
线程E,x =0
线程D,x =0
线程C,x =0
线程C,x =1
线程A,x =1
线程B,x =1
线程E,x =1
线程D,x =1
线程E,x =2
线程C,x =2
线程A,x =2
线程D,x =2
线程B,x =2
线程E,x =3
线程A,x =3
线程C,x =3
线程D,x =3
线程B,x =3
线程D,x =4
线程E,x =4
线程C,x =4
线程A,x =4
线程B,x =4

线程优先级

设置优先级:public final void setPriority(int newPriority)
取得优先级:public final int getPriority()
使用int返回,有三个优先级
最高:public static final int MAX_PRIORITY 10
中等:public static final int NORM_PRIORITY 5
最低:public static final int MIN_PRIORITY 1

package Demo;

class Mythread implements Runnable{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int x=0;x<20;x++){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+",x ="+x);
        }
    }
}
public class CollableDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        Mythread mt = new Mythread();
        Thread t1 = new Thread(mt,"线程A");
        Thread t2 = new Thread(mt,"线程B");
        Thread t3 = new Thread(mt,"线程C");
        t3.setPriority(Thread.MAX_PRIORITY);
        t1.start();
        t2.start();
        t3.start();
    }
}

相关文章

  • iOS多线程 NSOperation

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程 pthread、NSThread

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程: GCD

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程运用

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • iOS多线程基础

    系列文章: 多线程 多线程 pthread、NSThread 多线程 GCD 多线程 NSOperation 多线...

  • 多线程介绍

    一、进程与线程 进程介绍 线程介绍 线程的串行 二、多线程 多线程介绍 多线程原理 多线程的优缺点 多线程优点: ...

  • iOS进阶之多线程管理(GCD、RunLoop、pthread、

    深入理解RunLoopiOS多线程--彻底学会多线程之『GCD』iOS多线程--彻底学会多线程之『pthread、...

  • iOS多线程相关面试题

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • 多线程之--NSOperation

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

  • iOS多线程之--NSThread

    iOS多线程demo iOS多线程之--NSThread iOS多线程之--GCD详解 iOS多线程之--NSOp...

网友评论

      本文标题:多线程

      本文链接:https://www.haomeiwen.com/subject/axvhxxtx.html