Implementation of Java multithreading

Inherit thread class

public class Thread implements Runnable
public class NewThreadName extends Thread
{    //NewThreadName 类继承自 Thread 类
    public void run()
    {
        //线程的执行代码在这里
    }
}
new NewThreadName().start();    //NewThreadName 为继承自 Thread 的子类

Example 1

public class MyThread extends Thread
{
    @Override
    public void run()
    {
        super.run();
        System.out.println("这是线程类 MyThread");
    }
}
public static void main(String[] args)
{
    MyThread mythread=new MyThread();    //创建一个线程类
    mythread.start();    //开启线程
    System.out.println("运行结束!");    //在主线程中输出一个字符串
}
运行结束!
这是线程类 MyThread

Example 2

package ch14;
public class MyThread01 extends Thread
{
    @Override 
    public void run()
    { 
        try
        { 
            for(int i=0;i<10;i++)
            { 
                int time=(int)(Math.random()*1000); 
                Thread.sleep(time); 
                System.out.println("当前线程名称="+Thread.currentThread().getName()); 
            } 
        }
        catch(InterruptedException e)
        { 
            e.printStackTrace(); 
        } 
    } 
}
package ch14;
public class Test02
{
    public static void main(String[] args)
    { 
        try
        { 
            MyThread01 thread=new MyThread01(); 
            thread.setName("myThread"); 
            thread.start(); 
            for (int i=0;i<10;i++)
            { 
                int time=(int)(Math.random()*1000); 
                Thread.sleep(time); 
                System.out.println("主线程名称="+Thread.currentThread().getName()); 
            } 
        }
        catch(InterruptedException e)
        { 
            e.printStackTrace(); 
        }
    }
}
当前线程名称=myThread
主线程名称=main
当前线程名称=myThread
当前线程名称=myThread
当前线程名称=myThread
主线程名称=main
当前线程名称=myThread
当前线程名称=myThread
主线程名称=main
当前线程名称=myThread
主线程名称=main
当前线程名称=myThread
当前线程名称=myThread
当前线程名称=myThread
主线程名称=main
主线程名称=main
主线程名称=main
主线程名称=main
主线程名称=main
主线程名称=main

Example 3

package ch14;
public class MyThread02 extends Thread
{
    private int i; 
    public MyThread02(int i)
    { 
        super(); 
        this.i=i; 
    } 
    @Override 
    public void run()
    { 
        System.out.println("当前数字:"+i); 
    }
}
package ch14;
public class Test03
{
    public static void main(String[] args)
    { 
        MyThread02 t11=new MyThread02(1); 
        MyThread02 t12=new MyThread02(2); 
        MyThread02 t13=new MyThread02(3); 
        MyThread02 t14=new MyThread02(4); 
        MyThread02 t15=new MyThread02(5); 
        MyThread02 t16=new MyThread02(6); 
        MyThread02 t17=new MyThread02(7); 
        MyThread02 t18=new MyThread02(8); 
        MyThread02 t19=new MyThread02(9); 
        MyThread02 t110=new MyThread02(10); 
        t11.start(); 
        t12.start(); 
        t13.start(); 
        t14.start(); 
        t15.start(); 
        t16.start(); 
        t17.start(); 
        t18.start(); 
        t19.start(); 
        t110.start(); 
    }
}
当前数字:1
当前数字:3
当前数字:5
当前数字:7
当前数字:6
当前数字:2
当前数字:4
当前数字:8
当前数字:10
当前数字:9

Implement runnable interface

public class thread extends Object implements Runnable

Example 4

package ch14;
public class MyRunnable implements Runnable
{
    @Override 
    public void run()
    { 
        System.out.println("MyRunnable运行中!"); 
    }
}
package ch14;
public class Test04
{
    public static void main(String[] args)
    {
        Runnable runnable=new MyRunnable();
        Thread thread=new Thread(runnable);
        thread.start();
        System.out.println("主线程运行结束!");
    }
}
主线程运行结束!
MyRunnable运行中!

Comparison of two methods

1. Inherit the advantages and disadvantages of thread class

Thread.currenThread().sleep();

2. Advantages and disadvantages of implementing runnable interface

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>