创建线程:
1 可以实现Runnable接口。
2 可以扩展Thread类本身。
通过实现Runnable创建线程:

创建一个线程,最简单的方法是创建一个实现Runnable接口的类。

为了实现Runnable,这个类需要实现只有一个单一的方法 run(),它是这样声明的:

public void run( )
定义构成新线程 run()方法的代码内部。重要的是要明白的run()可以调用其他方法,使用其他类,并声明变量,就像主线程可以是很重要的。 
当创建一个实现Runnable类,会从类中实例化线程的对象。线程定义了多个构造函数。我们将使用一个如下所示:
Thread(Runnable threadOb, String threadName);
在这里,threadOb是实现Runnable接口和新线程的名称是由threadName指定一个类的实例。
创建新线程后,它不会启动运行,直到调用它的start()方法,它是内线程声明。start()方法如下所示:
void start( ); 

 下面是创建一个新的线程并开始运行一个例子:

// Create a new thread.
class NewThread implements Runnable {
Thread t;
NewThread() {
// Create a new, second thread
t = new Thread(this, "Demo Thread");
System.out.println("Child thread: " + t);
t.start(); // Start the thread
} // This is the entry point for the second thread.
public void run() {
try {
for(int i = 5; i > 0; i--) {
System.out.println("Child Thread: " + i);
// Let the thread sleep for a while.
Thread.sleep(50);
}
} catch (InterruptedException e) {
System.out.println("Child interrupted.");
}
System.out.println("Exiting child thread.");
}
} public class ThreadDemo {
public static void main(String args[]) {
new NewThread(); // create a new thread
try {
for(int i = 5; i > 0; i--) {
System.out.println("Main Thread: " + i);
Thread.sleep(100);
}
} catch (InterruptedException e) {
System.out.println("Main thread interrupted.");
}
System.out.println("Main thread exiting.");
}
}

这将产生以下结果:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting. 

 
 通过扩展Thread创建线程:
创建一个线程的第二种方式是创建可扩展条条一个新的类,然后创建一个类的实例。
扩展类必须重写run()方法,这是切入点的新线程。它还必须调用start()开始执行新线程。
例子:
下面是重写扩展线程前面的程序:
// Create a second thread by extending Thread
class NewThread extends Thread {
   NewThread() {
      // Create a new, second thread
      super("Demo Thread");
      System.out.println("Child thread: " + this);
      start(); // Start the thread
   }

// This is the entry point for the second thread.
   public void run() {
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Child Thread: " + i);
// Let the thread sleep for a while.
            Thread.sleep(50);
         }
      } catch (InterruptedException e) {
         System.out.println("Child interrupted.");
      }
      System.out.println("Exiting child thread.");
   }
}

public class ExtendThread {
   public static void main(String args[]) {
      new NewThread(); // create a new thread
      try {
         for(int i = 5; i > 0; i--) {
            System.out.println("Main Thread: " + i);
            Thread.sleep(100);
         }
      } catch (InterruptedException e) {
         System.out.println("Main thread interrupted.");
      }
      System.out.println("Main thread exiting.");
   }
}
这将产生以下结果:

Child thread: Thread[Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Exiting child thread.
Main Thread: 2
Main Thread: 1
Main thread exiting. 

java分享第八天-01(线程)的更多相关文章

  1. 【Java分享客栈】SpringBoot线程池参数搜一堆资料还是不会配,我花一天测试换你此生明白。

    一.前言   首先说一句,如果比较忙顺路点进来的,可以先收藏,有时间或用到了再看也行:   我相信很多人会有一个困惑,这个困惑和我之前一样,就是线程池这个玩意儿,感觉很高大上,用起来很fashion, ...

  2. java分享第九天-01(抽象类)

    1 为什么需要抽象类?如何定义抽象类 是一种模板模式,抽象类为所有子类提供了一个通用模板,子类可以在这个模版基础上进行扩展: 通过抽象类,可以避免子类设计的随意性.通过抽象类,我们就可以做到严格限制子 ...

  3. Java多线程| 01 | 线程概述

    Java多线程| 01 | 线程概述 线程相关概念 进程与线程 进程:进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是操作系统进行资源分配与调度的基本单位.可以把进程简单的理解 ...

  4. Java企业实训 - 01 - Java前奏

    前言: 虽然个人专攻.NET方向,不过由于个人是干教育行业的,方方面面的东西,不能说都必须精通,但肯定多少都会涉及到. 一个菜鸟学员,从啥都不会,经过一步步学习,最后到企业上手掌管一个模块甚至一个项目 ...

  5. 【Java分享客栈】一文搞定京东零售开源的AsyncTool,彻底解决异步编排问题。

    一.前言 本章主要是承接上一篇讲CompletableFuture的文章,想了解的可以先去看看案例: https://juejin.cn/post/7091132240574283813 Comple ...

  6. Java并发编程学习:线程安全与锁优化

    本文参考<深入理解java虚拟机第二版> 一.什么是线程安全? 这里我借<Java Concurrency In Practice>里面的话:当多个线程访问一个对象,如果不考虑 ...

  7. Java多线程并发04——合理使用线程池

    在此之前,我们已经了解了关于线程的基本知识,今天将为各位带来,线程池这一技术.关注我的公众号「Java面典」了解更多 Java 相关知识点. 为什么使用线程池?线程池做的工作主要是控制运行的线程的数量 ...

  8. Java并发编程实战 01并发编程的Bug源头

    摘要 编写正确的并发程序对我来说是一件极其困难的事情,由于知识不足,只知道synchronized这个修饰符进行同步. 本文为学习极客时间:Java并发编程实战 01的总结,文章取图也是来自于该文章 ...

  9. Java:并发笔记-01

    Java:并发笔记-01 说明:这是看了 bilibili 上 黑马程序员 的课程 java并发编程 后做的笔记 1. 进程与线程 本章内容 进程和线程的概念 并行和并发的概念 线程基本应用 1.1 ...

随机推荐

  1. centos安装PHP服务器步骤

    方法一.使用网友开发的EZHTTP程序包一键安装. 可以参考地址http://www.centos.bz/2013/08/ezhttp-tutorial/ http://www.cnblogs.com ...

  2. PHP模板引擎正则替换函数 preg_replace 与 preg_replace_callback 使用总结

    在编写PHP模板引擎工具类时,以前常用的一个正则替换函数为 preg_replace(),加上正则修饰符 /e,就能够执行强大的回调函数,实现模板引擎编译(其实就是字符串替换). 详情介绍参考博文:P ...

  3. C和指针 第三章 指针常量与常量指针

    c语言中声明常量的两种方式 const int value int const value 如果要声明常量的指针,即指向常量的指针,则可以参考上面的常量声明修改一下 const int *ptr in ...

  4. jsonp 自己写的一个例子

    function test(){ alert("13"); $.ajax({ type : "GET", async:false, url : "ht ...

  5. r-cnn学习(六):RPN及AnchorTargetLayer学习

    RPN网络是faster与fast的主要区别,输入特征图,输出region proposals以及相应的分数. # ------------------------------------------ ...

  6. linux 配置ssh免密码登陆本机

    1.安装 sudo apt-get install ssh 2.配置无密码登录 ssh-keygen -t rsa 遇到停顿按回车即可 进入/home/zeze/.ssh目录(隐藏目录,在winSCP ...

  7. shutil复制粘贴和压缩

    shutil复制粘贴和压缩 shutil 高级的文件.文件夹.压缩包处理模块 @1).将文件内容拷贝到另一个文件中 import shutil shutil.copyfileobj(open(&quo ...

  8. uoj228 基础数据结构练习题

    趁别人题解没有放出来赶快写一篇 整数序列,操作 区间加 区间变成sqrt(下取整) 区间和 考虑一下对于每个区间里所有sqrt不同的段操作,那么可以在O(段数logn)一次的时间内完成sqrt操作.考 ...

  9. log4Net不能成功生成日志问题(关于配置错误)

    log4Net不能成功生成日志问题(关于配置错误) 调试发现问题原因在于  Log4Net IsInfoEnabled 一直为 false,返回的对象中所有 IsXXX 一直为false,这个问题的原 ...

  10. iframe使用方法

    --点击按钮会把地址里的页面显示在oframe里,对iframe可以设置宽和高<iframe src="demo_iframe.htm" name="iframe_ ...