class MyThread extends Thread{
@Override
public void run() {
//此时只是定义处理一个线程类,在系统中总是还没有创建出 新的线程。
System.out.println("hello thread");
}
}
public class TestDemo11 {
public static void main(String[] args) {
//创建线程
Thread t = new MyThread();
//启动线程,在系统中创建出了新的线程
t.start();
}
}
登入後複製
執行緒之間是並發執行的
class MyThread3 extends Thread{
@Override
public void run() { //定义一个线程类
while (true) {
System.out.println("hello thread");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class TestDemo13 {
public static void main(String[] args) {
Thread t = new MyThread3();
t.start();//启动t线程
while(true){
System.out.println("hello main");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
//Runnable其实就是描述一个任务
//创建一个类,实现Runnable接口,重写Runnable类中的run方法,在run()方法中,描述的是该线程要指向的哪些任务。
class MyThread2 implements Runnable{
@Override
public void run() {
System.out.println("hello thread");
}
}
public class TestDemo12 {
public static void main(String[] args) {
//创建线程,把创建好的Runnable实例传给Thread实例
Thread t = new Thread(new MyThread2());
t.start();
}
}
登入後複製
方法三:利用內部類別
方法三其實是方法一個的翻版,就是把上面的兩種程式碼,改成了匿名內部類別。
public class TestDemo14 {
public static void main(String[] args) {
Thread t1 = new MyThread(){
@Override
public void run() {
System.out.println("hello thread1");
}
};
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("hello thread2");
}
});
t1.start();
t2.start();
}
}
登入後複製
方法四:使用lambmd表達式
public class TestDemo15 {
public static void main(String[] args) {
//其实就是使用lambad表达式代替Runnable
Thread t1 = new Thread(()->{
//()表示无参的run()方法
System.out.println("hello thread1");
});
}
}
public class TestDemo16 {
public static void func1() throws InterruptedException {
long big = System.currentTimeMillis();
//串行执行
Thread t = new Thread(()->{
long a = 0;
for(long i = 0;i<10_0000_0000;i++){
a++;
}
});
t.start();
t.join();
long end = System.currentTimeMillis();
System.out.println("串行消耗时间:" + (end - big) + "ms");
}
public static void func2() throws InterruptedException {
long big = System.currentTimeMillis();
Thread t1 = new Thread(()->{
long b = 0;
for(long i = 0;i< 10_0000_0000 / 2;i++){
b++;
}
});
t1.start();
Thread t2 = new Thread(()->{
long c = 0;
for(long i = 0;i<10_0000_0000/ 2;i++){
c++;
}
});
t2.start();
t1.join();
t2.join();
long end = System.currentTimeMillis();
System.out.println("并行执行消耗时间:" + (end - big) + "ms");
}
public static void main(String[] args) throws InterruptedException {
func1();//串行执行
func2();//并行执行
}
}
public class TestDemo18 {
public static void main(String[] args) {
Thread t = new Thread(()->{
System.out.println("hello thread");
});
t.start();
System.out.println(t.isDaemon());
}
}
//因为我们创建的是一个前台线程,所以返回false
public class TestDemo22 {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> public static void main(String[] args) throws InterruptedException {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> Thread t = new Thread(()->{<!--{C}%3C!%2D%2D%20%2D%2D%3E--> for(int i = 0;i<5;i++){<!--{C}%3C!%2D%2D%20%2D%2D%3E--> System.out.println("hello thread"); try {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> Thread.sleep(1000); } catch (InterruptedException e) {<!--{C}%3C!%2D%2D%20%2D%2D%3E--> e.printStackTrace(); } } }); t.start(); t.join();//main线程调用t.join()方法,main线程就处于阻塞状态,当t线程执行完后,唤醒main线程执行后序代码 System.out.println("hello main"); }}
登入後複製
获取线程的引用
使用方法Thread.currentTread()就可以该线程的实例。
public class TestDemo23 {
public static void main(String[] args) {
Thread t = new Thread(){
@Override
public void run() {
//获取当前线程的引用
//System.out.println(Thread.currentThread().getName());
//因为当前使用的匿名内部类是继承自Thread类,Thread就是该匿名内部类的父类,所以可以通过this得到当前Thread的实例
System.out.println(this.getName());
}
};
t.start();
}
}