栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

线程 创建(2)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

线程 创建(2)

public class ThreadDemo3 {
    public static void main(String[] args)
    {//Thread mainThread=Thread.currentThread();
        //  System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
     thread.start();
    }}
class MyThread extends Thread
{public void run()
{Thread thread=Thread.currentThread();
    System.out.println("线程的名称:"+thread.getName());
}

}

`public class ThreadDemo3 {
public static void main(String[] args)
{Thread mainThread=Thread.currentThread();
System.out.println(“线程名称:”+mainThread.getName());
Thread thread=new MyThread();
thread.start();
}}
class MyThread extends Thread
{public void run()
{Thread thread=Thread.currentThread();
System.out.println(“线程的名称:”+thread.getName());
}

}
`

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
        thread.start();
    }}
class MyThread extends Thread
{public void run()
{Thread thread=Thread.currentThread();
try
{Thread.sleep(60*60*1000);
}   catch(InterruptedException e)
{e.printStackTrace();
}
 System.out.println("线程的名称:"+thread.getName());
}

}

最后执行的
在这里插入代码片

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
       // thread.start();
thread.run();
    }}
class MyThread extends Thread
{public void run()
{
System.out.println("你好,线程");
Thread thread=Thread.currentThread();
    //try
    //{Thread.sleep(60*60*1000);
    //}   catch(InterruptedException e)
    //{e.printStackTrace();
    //}
    //System.out.println("线程的名称:"+thread.getName());
}

}

在这里插入代码片
public class ThreadDemo3 {
public static void main(String[] args)
{Thread mainThread=Thread.currentThread();
System.out.println(“线程名称:”+mainThread.getName());
Thread thread=new MyThread();
thread.start();
//thread.run();
}}
class MyThread extends Thread
{public void run()
{
System.out.println(“你好,线程”);
Thread thread=Thread.currentThread();
//try
//{Thread.sleep(60601000);
//} catch(InterruptedException e)
//{e.printStackTrace();
//}
//System.out.println(“线程的名称:”+thread.getName());
}

}

`public class ThreadDemo3 {
public static void main(String[] args)
{Thread mainThread=Thread.currentThread();
System.out.println(“线程名称:”+mainThread.getName());
Thread thread=new MyThread();
// thread.start();
thread.run();
}}
class MyThread extends Thread
{public void run()
{
System.out.println(“你好,线程”);
Thread thread=Thread.currentThread();
//try
//{Thread.sleep(60601000);
//} catch(InterruptedException e)
//{e.printStackTrace();
//}
System.out.println(“线程的名称:”+thread.getName());
}

}
`

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
      thread.start();
//  thread.run();
    }}
class MyThread extends Thread
{public void run()
{
System.out.println("你好,线程");
Thread thread=Thread.currentThread();
    //try
    //{Thread.sleep(60*60*1000);
    //}   catch(InterruptedException e)
    //{e.printStackTrace();
    //}
   System.out.println("线程的名称:"+thread.getName());
}

}

执行run方法:使用main线程调用一个普通方法
执行start方法:创建新线程

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
      //   thread.start();
 thread.run();
thread.run();
    }}
class MyThread extends Thread
{public void run()
{
    System.out.println("你好,线程");
    Thread thread=Thread.currentThread();
    //try
    //{Thread.sleep(60*60*1000);
    //}   catch(InterruptedException e)
    //{e.printStackTrace();
    //}
    System.out.println("线程的名称:"+thread.getName());
}

}

`
public class ThreadDemo3 {
public static void main(String[] args)
{Thread mainThread=Thread.currentThread();
System.out.println(“线程名称:”+mainThread.getName());
Thread thread=new MyThread();
// thread.start();
thread.run();
thread.run();
thread.run();
}}
class MyThread extends Thread
{public void run()
{
System.out.println(“你好,线程”);
Thread thread=Thread.currentThread();
//try
//{Thread.sleep(60601000);
//} catch(InterruptedException e)
//{e.printStackTrace();
//}
System.out.println(“线程的名称:”+thread.getName());
}

}
`

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
        thread.start();
  //thread.run();
 //thread.run();
 //thread.run();
    }}
class MyThread extends Thread
{public void run()
{
    System.out.println("你好,线程");
    Thread thread=Thread.currentThread();
    //try
    //{Thread.sleep(60*60*1000);
    //}   catch(InterruptedException e)
    //{e.printStackTrace();
    //}
    System.out.println("线程的名称:"+thread.getName());
}

}

public class ThreadDemo3 {
    public static void main(String[] args)
    {Thread mainThread=Thread.currentThread();
        System.out.println("线程名称:"+mainThread.getName());
        Thread thread=new MyThread();
        thread.start();
        thread.start();
  //thread.run();
 //thread.run();
 //thread.run();
    }}
class MyThread extends Thread
{public void run()
{
    System.out.println("你好,线程");
    Thread thread=Thread.currentThread();
    //try
    //{Thread.sleep(60*60*1000);
    //}   catch(InterruptedException e)
    //{e.printStackTrace();
    //}
    System.out.println("线程的名称:"+thread.getName());
}

}

`1 继承创建 2 实现Runnble接口

public class ThreadDemo4 {
    public static void main(String[] args){}

}
class MyThread2 implements Runnable{
    public void run()
    {Thread thread=Thread.currentThread();
System.out.println();
    }
}

public class ThreadDemo4 {
    public static void main(String[] args){}
MyThread2 myThread2=new MyThread2();
Thread thread=new Thread(myThread2);
}
class MyThread2 implements Runnable{
    public void run()
    {Thread thread=Thread.currentThread();
System.out.println("线程执行:"+thread.getName());
    }
}

public class ThreadDemo4 {
    public static void main(String[] args){
    MyThread2 myThread2=new MyThread2();
    Thread thread=new Thread(myThread2);
thread.start();
}}
class MyThread2 implements Runnable{
    public void run()
    {Thread thread=Thread.currentThread();
        System.out.println("线程执行:"+thread.getName());
    }
}

public class ThreadDemo5 {
public static void main(String[] args)
{Thread thread=new Thread(new Runnable(){
public void run(){
Thread t=Thread.currentThread();
System.out.println("执行任务:"+t.getName());

}}
);thread.start();
}
}

public class ThreadDemo6 {
public static void main(String[] args)
{Thread thread=new Thread(()->{
Thread t=Thread.currentThread();
System.out.println("任务执行:"+t.getName());
});thread.start();
}
}

public class ThreadDemo7 {
public static void main(String[] args){
public void run(){
Thread t=Thread.currentThread();
System.out.println("任务执行:"+t.getName());
}
};
thread.start();
}
}

public class ThreadDemo7 {
public static void main(String[] args){
Thread thread=new Thread(){
public void run(){
Thread t=Thread.currentThread();
System.out.println("任务执行:"+t.getName());
}
};
thread.start();
}
}

public class ThreadDemo8 {
    public static void main(String[] args){}}

class MyThread3 implements Callable
{public Integer call() throws Exception
{int randomNum=new Random().nextInt(10);
    System.out.println(Thread.currentThread().getNama()+"-随机数:"+randomNum);
    return randomNum;
}
}

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

public class ThreadDemo8 {
    public static void main(String[] args){
        MyCallable mycallable=new MyCallable();
        FutureTask futureTask =new FutureTask<>(mycallable);
        Thread thread=new Thread(futureTask);
        thread.start();
    }}

class MyCallable implements Callable
{public Integer call() throws Exception
{int randomNum=new Random().nextInt(10);
    System.out.println(Thread.currentThread().getName()+"-随机数:"+randomNum);
    return randomNum;
}}

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

public class ThreadDemo8 {
    public static void main(String[] args){
        MyCallable mycallable=new MyCallable();
        FutureTask futureTask =new FutureTask<>(mycallable);
        Thread thread=new Thread(futureTask);
        thread.start();
int result=futureTask.get();
System.out.println("新线程返回的结果为:"+result)
    }}

class MyCallable implements Callable
{public Integer call() throws Exception
{int randomNum=new Random().nextInt(10);
    System.out.println(Thread.currentThread().getName()+"-随机数:"+randomNum);
    return randomNum;
}}

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

public class ThreadDemo8 {
    public static void main(String[] args){
        MyCallable mycallable=new MyCallable();
        FutureTask futureTask =new FutureTask<>(mycallable);
        Thread thread=new Thread(futureTask);
        thread.start();
int result=futureTask.get();
System.out.println("新线程返回的结果为:"+result);
    }}

class MyCallable implements Callable
{public Integer call() throws Exception
{int randomNum=new Random().nextInt(10);
    System.out.println(Thread.currentThread().getName()+"-随机数:"+randomNum);
    return randomNum;
}}

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

public class ThreadDemo9 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask=new FutureTask<>(new Callable{
        public String call() throws Exception
        {String[] arrs=new String[]{"Java","MySQL","Thread"};
            String result=arrs[new Random().nextInt(3)];
            System.out.println(Thread.currentThread().getName()+"-字符串:"+result);
            return result;
        }
    });
        Thread thread=new Thread(futureTask);
        thread.start();
        String result=futureTask.get();
        System.out.println(Thread.currentThread().getName()+"--新线程的返回值"+result);}
}

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

public class ThreadDemo9 {
public static void main(String[] args) throws ExecutionException, InterruptedException {
FutureTask futureTask=new FutureTask<>(new Callable() {

        public String call() throws Exception
        {String[] arrs=new String[]{"Java","MySQL","Thread"};
            String result=arrs[new Random().nextInt(3)];
            System.out.println(Thread.currentThread().getName()+"-字符串:"+result);
            return result;
        }
    });
    Thread thread=new Thread(futureTask);
    thread.start();
    String result=futureTask.get();
    System.out.println(Thread.currentThread().getName()+"--新线程的返回值"+result);}

}
`
jdk8以上版本推荐使用方法四
补充jconsole 视频

QQ录屏20220612111537

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/970543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号