zoukankan      html  css  js  c++  java
  • 初探Java多线程

    什么是多线程?这些话就不说了,直接看样例。

    一、  使用多线程

    1、  继承Thread类

    package com.ztz.myThread;
    
    public class MyThread extends Thread{
    	@Override
    	public void run() {
    		System.out.println("继承Thread");
    	}
    	public static void main(String[] args)throws Exception {
    		MyThread t=new MyThread();
    		t.start();
    	}
    }

    2、  实现Runnable接口

    package com.ztz.myThread;
    
    public class MyRunnable implements Runnable{
    	@Override
    	public void run() {
    		System.out.println("实现Runnable接口");
    	}
    	public static void main(String[] args)throws Exception {
    		MyRunnable runnable=new MyRunnable();
    		Thread thread=new Thread(runnable);
    		thread.start();
    	}
    }

    PS:线程的启动是start()方法,不是run()方法。继承Thread类的方式是有局限性的,由于java是单继承,为了改变这个局限性。我们能够实现Runnable接口来实现多线程


    二、  currentThread()方法

    currentThread()方法能够返回代码段正在被哪个线程调用的信息。


    package com.ztz.myThread;
    
    public class MyThread{
    	public static void main(String[] args)throws Exception {
    		System.out.println(Thread.currentThread().getName());
    	}
    }
    执行该方法,控制台输出:
    main

    说明:main方法被名为main线程调用。Thread.currentThread().getName()是获得线程名

    package com.ztz.myThread;
    
    public class MyThread extends Thread{
    	public MyThread() {
    		System.out.println("构造方法:"+Thread.currentThread().getName());
    	}
    	@Override
    	public void run() {
    		System.out.println("run:"+Thread.currentThread().getName());
    	}
    	public static void main(String[] args)throws Exception {
    		MyThread thread=new MyThread();
    		thread.start();
    	}
    }
    执行该方法控制台输出:
    构造方法:main
    run:Thread-0


    事实上,main也就是主线程了Thread-0是子线程。假设在启动个线程就是Thread-1了。


    三、  sleep()方法

    方法sleep()的作用是在指定的毫秒数内让当前正在运行的线程休眠(暂停运行),这个正在运行的线程是指this.currentThread()返回的线程。
    package com.ztz.myThread;
    
    public class MyThread extends Thread{
    	@Override
    	public void run() {
    		try{
    			System.out.println("run start:"+System.currentTimeMillis());
    			Thread.sleep(2000);
    			System.out.println("run end:"+System.currentTimeMillis());
    		}catch(Exception e){
    			
    		}
    	}
    	public static void main(String[] args)throws Exception {
    		MyThread thread=new MyThread();
    		System.out.println("thread start:"+System.currentTimeMillis());
    		thread.start();
    		System.out.println("thread end:"+System.currentTimeMillis());
    	}
    }

    输出结果:
    thread start:1437233204468
    thread end:1437233204468
    run start:1437233204468
    run end:1437233206468

    四、  getId()方法

    getId()方法的作用是取得线程的唯一标识。
    package com.ztz.myThread;
    
    public class MyThread{
    	public static void main(String[] args)throws Exception {
    		Thread currentThread = Thread.currentThread();
    		System.out.println(currentThread.getName()+"--->"+currentThread.getId());
    	}
    }

    输出结果:
    main--->1



  • 相关阅读:
    java Activiti 工作流引擎 SSM 框架模块设计方案
    自定义表单 Flowable 工作流 Springboot vue.js 前后分离 跨域 有代码生成器
    数据库设计的十个最佳实践
    activiti 汉化 stencilset.json 文件内容
    JAVA oa 办公系统模块 设计方案
    java 考试系统 在线学习 视频直播 人脸识别 springboot框架 前后分离 PC和手机端
    集成 nacos注册中心配置使用
    “感恩节 ”怼记
    仓颉编程语言的一点期望
    关于System.out.println()与System.out.print("\n")的区别
  • 原文地址:https://www.cnblogs.com/jhcelue/p/7070863.html
Copyright © 2011-2022 走看看