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



