Java实现多线程有两种方式:一是继承Thread类;二是实现Runable接口。

一、Thread实现

  1. publicclass ThreadDemo2 {
  2. publicstaticvoid main(String[] args) {
  3. new TestThread2().start();
  4. inti = 0;
  5. while(i++ < 100) {
  6. System.out.println("main thread is running");
  7. }
  8. }
  9. }
  10. class TestThread2 extends Thread {
  11. publicvoid run() {
  12. intj = 0;
  13. while(j++ < 100) {
  14. System.out.println(Thread.currentThread().getName() + " is running!");
  15. }
  16. }
  17. }

运行结果:

二、Runnable实现

  1. publicclass ThreadDemo3 {
  2. publicstaticvoid main(String[] args) {
  3. TestThread3 t3 = new TestThread3();
  4. Thread t = new Thread(t3);
  5. t.start();
  6. inti = 0;
  7. while(i++ < 100) {
  8. System.out.println("main thread is running");
  9. }
  10. }
  11. }
  12. class TestThread3 implements Runnable {
  13. publicvoid run() {
  14. intj = 0;
  15. while(j++ < 100) {
  16. System.out.println(Thread.currentThread().getName() + " is running!");
  17. }
  18. }
  19. }

运行结果:

Java笔记15:多线程的更多相关文章

  1. JAVA笔记14__多线程共享数据(同步)/ 线程死锁 / 生产者与消费者应用案例 / 线程池

    /** * 多线程共享数据 * 线程同步:多个线程在同一个时间段只能有一个线程执行其指定代码,其他线程要等待此线程完成之后才可以继续执行. * 多线程共享数据的安全问题,使用同步解决. * 线程同步两 ...

  2. Thinking in Java——笔记(15)

    Generics The term "generic" means "pertaining or appropriate to large groups of class ...

  3. java笔记--关于多线程如何查看JVM中运行的线程

    查看JVM中的线程 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3890280.html "谢谢-- ThreadGrou ...

  4. java笔记--关于多线程状态的理解和应用

    关于多线程的状态 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3890266.html  "谢谢-- 线程共有6种状态:1 ...

  5. java笔记15之this

    this:是当前类的对象引用,记为该类的一个对象 注意:谁调用这个方法,在这个方法内部的this就是代表谁 解决场景: 解决局部变量隐藏成员变量 class Student { private Str ...

  6. python 3.x 学习笔记15(多线程)

    1.线程进程进程:程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程,不具备执行感念,只是程序各种资源集合 线程:线程是操作系统能够进行运算调度的最小单 ...

  7. JAVA自学笔记15

    JAVA自学笔记15 @例题1:共有5个学生,请把五个学生的信息存储到数组中,并遍历数组,并获取每个学生的信息 Students[] students=new Student[5]; Student ...

  8. java笔记--多线程基础

    多线程技术 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3889579.html  "谢谢-- 在java中实现多线程技术 ...

  9. Java基础学习笔记: 多线程,线程池,同步锁(Lock,synchronized )(Thread类,ExecutorService ,Future类)(卖火车票案例)

    多线程介绍 学习多线程之前,我们先要了解几个关于多线程有关的概念.进程:进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. 线 ...

随机推荐

  1. Rotate Image——数学相关

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  2. OpenAcc笔记——update

    program main use omp_lib use openacc implicit none real,allocatable:: v1(:) integer length, idx call ...

  3. 史上最全的web前端系统学习教程!

    这份资料整理花了近7天,如果感觉有用,可以分享给更有需要的人. 在看接下的介绍前,我先说一下整理这份资料的初衷: 我的初衷是想帮助在这个行业发展的朋友和童鞋们,在论坛博客等地方少花些时间找资料,把有限 ...

  4. css3翻书效果

    强大的css3不需要解释,代码分层理解[直接复制],很有意思. 效果图: <ul class="align"> <li> <figure class= ...

  5. npp插件-NewFileBrowser:自定义模板

    ☞ 概述 NewFileBrowser用于定义文件的初始化文本.当我们创建一个jsp文件,或者spring bean的配置文件,或者maven的pom文件... 总有一些内容是固定的:把这些内容建立成 ...

  6. Word Ladder(LintCode)

    Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...

  7. 洛谷——P1618 三连击(升级版)

    P1618 三连击(升级版) 题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数的比例是A:B:C,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. //感谢 ...

  8. POJ 3662 Telephone Lines (分层图)

    Telephone Lines Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6785   Accepted: 2498 D ...

  9. POJ 3660 Cow Contest (dfs)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11129   Accepted: 6183 Desc ...

  10. 【大视野入门OJ】1099:歌德巴赫猜想

    Description 歌德巴赫猜想大家都很熟悉吧?给一个数,能够分解成两个素数的和.现在要给你一个n,6 <= n < 1000000,让你求他会分解成哪两个素数?如果存在多组解,则要求 ...