Java定义了两种创建线程的方法:

1.实现Runnable接口

要实现Runnable接口,只需简单地实现run()方法即可,声明如下:public void run()

在run()方法中,可以定义构成新线程的代码。需重点理解的是,run()可以调用其它方法、使用其他类和声明变量,就像主线程能做的那样。唯一的区别是:run()方法是程序的另一个并发的执行线程的进入店,这个线程在run()方法返回时结束。

创建新线程以后,直到调用了它的start()方法后才会执行。本质上,start()执行对run()的调用。

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

输出:

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
Exiting Main thread

2. 扩展Thread类本身

第二种创建线程的方法是创建一个扩展Thread类的新类,然后创建该类的一个实例。这个扩展的类必须重写run()方法,这是新线程的进入点,同时它也必须调用start()方法来执行线程。

// Create a second thread.
public class NewThread extends Thread{ NewThread(){
super("Demo thread");
System.out.println("Child thread: "+this);
start();
} // This is the entry point of the second thread.
public void run(){
try{
for(int i=5;i>0;i--){
System.out.println("Child thread: "+i);
Thread.sleep(500);
}
}
catch(InterruptedException e){
System.out.println("Child thread interrupted");
}
System.out.println("Exiting child thread");
}
} class ThreadDemo{
public static void main(String args[]){
new NewThread();
try{
for(int i=5;i>0;i--){
System.out.println("Main thread: "+i);
Thread.sleep(1000);
}
}
catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
System.out.println("Exiting Main thread");
}
}

输出:

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
Exiting Main thread

同步:当两个或多个线程需要访问同一共享资源时,需要某种方式来确保资源在某一时刻只被一个线程使用,这个方式称为同步。

不使用同步的例子:

class Callme{
void call(String msg){
System.out.print("["+msg);
try{
Thread.sleep(1000);
}
catch(InterruptedException e){
System.out.println("Interrupted");
}
System.out.println("]");
}
} class Caller implements Runnable{
String msg;
Callme target;
Thread t;
public Caller(Callme targ, String s){
target=targ;
msg=s;
t=new Thread(this);
t.start();
} public void run(){
target.call(msg);
}
} public class Synch {
public static void main(String args[]){
Callme target=new Callme();
Caller ob1=new Caller(target,"Hello");
Caller ob2=new Caller(target,"Synchronized");
Caller ob3=new Caller(target,"World"); try{
ob1.t.join();
ob2.t.join();
ob3.t.join();
}
catch(InterruptedException e){
System.out.println("Interrupted");
}
}
}

输出:

[Hello[World[Synchronized]
]
]

使用同步,只需简单地在call()的定义前面加上关键字synchronized即可

class Callme{
synchronized void call(String msg){

输出:

[Hello]
[Synchronized]
[World]

当不能在类相应的方法中添加synchronized关键字时,只需要将对该类方法的访问置于一个synchronized快中

public void run(){
synchronized(target){
target.call(msg);
}
}

Java:创建线程的更多相关文章

  1. -1-5 java 多线程 概念 进程 线程区别联系 java创建线程方式 线程组 线程池概念 线程安全 同步 同步代码块 Lock锁 sleep()和wait()方法的区别 为什么wait(),notify(),notifyAll()等方法都定义在Object类中

     本文关键词: java 多线程 概念 进程 线程区别联系 java创建线程方式 线程组 线程池概念 线程安全 同步 同步代码块 Lock锁  sleep()和wait()方法的区别 为什么wait( ...

  2. 操作系统实现线程的几种模式 和 java创建线程的3个方式

    操作系统实现线程的几种模式 和 java创建线程的3个方式  这是两个概念 在操作系统中,线程可以实现在用户模式下,也可以实现在内核模式下,也可以两者结合实现. 1.实现线程的三种方式: (1)继承t ...

  3. Java并发编程:Java创建线程的三种方式

    目录 引言 创建线程的三种方式 一.继承Thread类 二.实现Runnable接口 三.使用Callable和Future创建线程 三种方式的对比 引言 在日常开发工作中,多线程开发可以说是必备技能 ...

  4. Java创建线程的三种主要方式

    Java创建线程的主要方式 一.继承Thread类创建 通过继承Thread并且重写其run(),run方法中即线程执行任务.创建后的子类通过调用 start() 方法即可执行线程方法. 通过继承Th ...

  5. Java创建线程的四种方式

    Java创建线程的四种方式 1.继承Thread类创建线程 定义Thread类的子类,并重写该类的run方法,run()方法的内容就是该线程执行的内容 创建Thread子类的实例,即创建了线程对象. ...

  6. 当阿里面试官问我:Java创建线程有几种方式?我就知道问题没那么简单

    这是最新的大厂面试系列,还原真实场景,提炼出知识点分享给大家. 点赞再看,养成习惯~ 微信搜索[武哥聊编程],关注这个 Java 菜鸟. 昨天有个小伙伴去阿里面试实习生岗位,面试官问他了一个老生常谈的 ...

  7. java创建线程的多种方式

    java创建线程的四种方式 1.继承 Thread 类 通过继承 Thread 类,并重写它的 run 方法,我们就可以创建一个线程. 首先定义一个类来继承 Thread 类,重写 run 方法. 然 ...

  8. Java创建线程的细节分析

    转载:http://shmilyaw-hotmail-com.iteye.com/blog/1880902 前言 关于线程创建的问题,可以说是老生常谈了.在刚开始学习Thread的时候基本上都会接触到 ...

  9. java创建线程的三种方法

    这里不会贴代码,只是将创建线程的三种方法做个笼统的介绍,再根据源码添加上自己的分析. 通过三种方法可以创建java线程: 1.继承Thread类. 2.实现Runnable接口. 3.实现Callab ...

  10. java创建线程

    创建一个线程 Java提供了两种创建线程方法: 通过实现Runable接口: http://blog.csdn.net/duruiqi_fx/article/details/52187275 通过继承 ...

随机推荐

  1. jQuery 3D旋转展示焦点图

    在线演示 本地下载

  2. c# XML-Object对象 序列化-反序列化

    using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...

  3. Contiki事件

    一.事件相关数据结构 1.事件结构体 struct event_data { process_event_t ev; process_data_t data; struct process *p; } ...

  4. Spark- Linux下安装Spark

    Spark- Linux下安装Spark 前期部署 1.JDK安装,配置PATH 可以参考之前配置hadoop等配置 2.下载spark-1.6.1-bin-hadoop2.6.tgz,并上传到服务器 ...

  5. matlab的代码注释

    1.注释一块代码: %{ 此处代码块 %} 2.注释数行代码: 先选中,然后用组合键Ctrl+R 取消注释,用组合键Ctrl+T 3.双%%的作用:代码分块运行,点击双%%之间的代码,再点Run Se ...

  6. BZOJ 1634 [Usaco2007 Jan]Protecting the Flowers 护花:贪心【局部分析法】

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1634 题意: 约翰留下他的N只奶牛上山采木.可是,当他回来的时候,他看到了一幕惨剧:牛们正 ...

  7. findbugs规则

    FindBugs是基于Bug Patterns概念,查找javabytecode(.class文件)中的潜在bug,主要检查bytecode中的bug patterns,如NullPoint空指针检查 ...

  8. SpringBoot_00_资源汇总贴

    一.精选 1.Spring Boot文档 二.参考资料 1.springboot-learning-example 2.Spring boot 那些事 3.Spring Boot干货系列 4.Spri ...

  9. hdu 6109 数据分割

    /** * 题目描述有点坑,勉强能读懂,大致意思,有多组约束条件.原本每组数据之间是有分界符号的 * 现在分界符号没了,让你找出原来每组数据多少个条件,并且告诉,每组的最后一个条件会使得与前面的 * ...

  10. 二:apache的Qpid消息中间件介绍

    一:什么是Qpid?--->Qpid 是 Apache 开发的一款面向对象的消息中间件,它是一个 AMQP 的实现,可以和其他符合 AMQP 协议的系统进行通信.--->Qpid 提供了 ...