1.继承Thread类(不推荐)

代码很简单,就不说了

public class ThreadTest02 {

    public static void main(String[] args) {
new UserThread("A").start();
System.out.println("结束。。。");
} public static class UserThread extends Thread {
UserThread(String name) {
this.setName(name);
} public void run() {
System.out.println(Thread.currentThread().getName() + " run方法执行了。。。");
}
}
}

2.实现Runable接口

public class RunableTest01 {

    public static void main(String[] args) {
Thread thread = new Thread(new UserThread(), "A");
thread.start();
System.out.println("结束。。。");
} public static class UserThread implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName() + " run方法执行了");
}
}
}

以上运行的结果:

结束。。。
A run方法执行了

run和start的区别?

  • start()它的作用是启动一个新线程。通过start()方法来启动的新线程,处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行相应线程的run()方法,这里方法run()称为线程体,它包含了要执行的这个线程的内容,run方法运行结束,此线程随即终止。start()不能被重复调用。用start方法来启动线程,真正实现了多线程运行,即无需等待某个线程的run方法体代码执行完毕就直接继续执行下面的代码。这里无需等待run方法执行完毕,即可继续执行下面的代码,即进行了线程切换。
  • run()就和普通的成员方法一样,可以被重复调用。如果直接调用run方法,并不会启动新线程!程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到多线程的目的。总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。
  总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。
 

 

数据共享与非共享

1.数据不共享

新起三个不同的线程,对成员变量操作,互不影响

public class ThreadShareTest01 {

    public static void main(String[] args) {

        Thread thread1 = new Thread(new UserThread(), "A");
Thread thread2 = new Thread(new UserThread(), "B");
Thread thread3 = new Thread(new UserThread(), "C"); thread1.start();
thread2.start();
thread3.start(); } public static class UserThread extends Thread {
private int count = 10; public void run() {
while (count > 0) {
count--;
System.out.println(Thread.currentThread().getName() + " 线程过来了,count变为:" + count);
}
System.out.println("count小于0了");
}
} }

结果:

A 线程过来了,count变为:9
A 线程过来了,count变为:8
A 线程过来了,count变为:7
A 线程过来了,count变为:6
A 线程过来了,count变为:5
A 线程过来了,count变为:4
A 线程过来了,count变为:3
A 线程过来了,count变为:2
A 线程过来了,count变为:1
A 线程过来了,count变为:0
count小于0了
B 线程过来了,count变为:9
B 线程过来了,count变为:8
B 线程过来了,count变为:7
B 线程过来了,count变为:6
B 线程过来了,count变为:5
B 线程过来了,count变为:4
B 线程过来了,count变为:3
B 线程过来了,count变为:2
B 线程过来了,count变为:1
B 线程过来了,count变为:0
count小于0了
C 线程过来了,count变为:9
C 线程过来了,count变为:8
C 线程过来了,count变为:7
C 线程过来了,count变为:6
C 线程过来了,count变为:5
C 线程过来了,count变为:4
C 线程过来了,count变为:3
C 线程过来了,count变为:2
C 线程过来了,count变为:1
C 线程过来了,count变为:0
count小于0了

2.数据共享

public class ThreadShareTest02 {

    public static void main(String[] args) {

        UserThread userThread = new UserThread();

        Thread thread1 = new Thread(userThread, "A");
Thread thread2 = new Thread(userThread, "B");
Thread thread3 = new Thread(userThread, "C");
Thread thread4 = new Thread(userThread, "D");
Thread thread5 = new Thread(userThread, "E");
Thread thread6 = new Thread(userThread, "F");
Thread thread7 = new Thread(userThread, "G");
Thread thread8 = new Thread(userThread, "H");
Thread thread9 = new Thread(userThread, "I");
Thread thread10 = new Thread(userThread, "J"); thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
thread6.start();
thread7.start();
thread8.start();
thread9.start();
thread10.start(); } public static class UserThread extends Thread {
private int count = 10; public void run() {
count--;
System.out.println(Thread.currentThread().getName() + " 线程过来了,count变为:" + count);
}
} }

结果:

有问题了!

A 线程过来了,count变为:8
B 线程过来了,count变为:8
C 线程过来了,count变为:7
E 线程过来了,count变为:5
D 线程过来了,count变为:5
J 线程过来了,count变为:3
G 线程过来了,count变为:3
F 线程过来了,count变为:2
I 线程过来了,count变为:1
H 线程过来了,count变为:0

解决办法?

最简单的是在run方法加上synchronized,其实还有别的更高效的方法,后面会说到

        public synchronized void run() {
count--;
System.out.println(Thread.currentThread().getName() + " 线程过来了,count变为:" + count);
}

改善结果:

A 线程过来了,count变为:9
B 线程过来了,count变为:8
D 线程过来了,count变为:7
E 线程过来了,count变为:6
C 线程过来了,count变为:5
G 线程过来了,count变为:4
H 线程过来了,count变为:3
I 线程过来了,count变为:2
J 线程过来了,count变为:1
F 线程过来了,count变为:0

 ok!

 

并发编程:Thread和Runable-01的更多相关文章

  1. java 并发编程——Thread 源码重新学习

    Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...

  2. Java并发编程-Thread类的使用

    在前面2篇文章分别讲到了线程和进程的由来.以及如何在Java中怎么创建线程和进程.今天我们来学习一下Thread类,在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着 ...

  3. C++并发编程 thread

    std::thread C++11在标准库中为多线程提供组件, 使用线程需要包含头文件 thread, 其命名空间为 std. 启动新线程 每个进程至少有一个线程: 执行main()函数的线程, 其余 ...

  4. Java 并发编程:Callable和Future

    项目中经常有些任务需要异步(提交到线程池中)去执行,而主线程往往需要知道异步执行产生的结果,这时我们要怎么做呢?用runnable是无法实现的,我们需要用callable实现. import java ...

  5. Java 并发编程——Callable+Future+FutureTask

    Java 并发编程系列文章 Java 并发基础——线程安全性 Java 并发编程——Callable+Future+FutureTask java 并发编程——Thread 源码重新学习 java并发 ...

  6. Java并发编程(08):Executor线程池框架

    本文源码:GitHub·点这里 || GitEE·点这里 一.Executor框架简介 1.基础简介 Executor系统中,将线程任务提交和任务执行进行了解耦的设计,Executor有各种功能强大的 ...

  7. Java 并发编程——Executor框架和线程池原理

    Eexecutor作为灵活且强大的异步执行框架,其支持多种不同类型的任务执行策略,提供了一种标准的方法将任务的提交过程和执行过程解耦开发,基于生产者-消费者模式,其提交任务的线程相当于生产者,执行任务 ...

  8. java并发编程——通过ReentrantLock,Condition实现银行存取款

         java.util.concurrent.locks包为锁和等待条件提供一个框架的接口和类,它不同于内置同步和监视器.该框架允许更灵活地使用锁和条件,但以更难用的语法为代价. Lock 接口 ...

  9. Java并发编程——BlockingQueue

    简介 BlockingQueue很好的解决了多线程中,如何高效安全"传输"数据的问题.通过这些高效并且线程安全的队列类,为我们快速搭建高质量的多线程程序带来极大的便利. 阻塞队列是 ...

随机推荐

  1. Uncaught Error: `setOption` should not be called during main process.

    主要是自己记笔记用,大佬些莫怪! 使用 echart 出现 :Uncaught Error: `setOption` should not be called during main process. ...

  2. 【转帖】IP地址总数

    http://blog.sina.com.cn/s/blog_ac1d70a40102xn8z.html 计算规则挺有意思的 私有IP地址段: A类:10.0.0.0到10.255.255.255 1 ...

  3. 人工智能对人类有哪些影响 选择Python入门怎样

    人工智能对人类有哪些影响?选择Python入门怎样?人工智能是科技时代进步的产物,也是目前人们非常关注的一个产业.那么,随着人工智能的发展,对人类生活的有哪些影响呢? 1.人工智能对文化产业影响 据了 ...

  4. python 并发的开端

    目录 网络并发 进程的基础 2.操作系统 操作系统的发展史 多道技术 第二代 1955~1965 磁带存储--批处理系统 第三代集成电路,多道程序系统(1955~1965) 进程的理论(重点) 2.操 ...

  5. 搭建hexo静态博客

    使用hexo搭建博客,并将博客部署到github 需要的工具 Node.js Git 一个Github账号 正式开始 在任意目录下新建一个文件夹,如blog,在该文件夹下右键打开git bash he ...

  6. DO、VO、DTO 区别

    DTO:数据传输对象,主要用于外部接口参数传递封装,接口与接口进行传递使用. VO:视图对象,主要用于给前端返回页面参数使用. DO:数据对象,主要用于数据库层传递. DTO转DO:接口接收参数将参数 ...

  7. 使用winsw将springboot打包的jar注册系统本地服务

    1.下载winsw 下载地址:https://github.com/kohsuke/winsw/releases 我这里下载的是2.3.0版. 下载sample-minimal.xml和WinSW.N ...

  8. 在论坛中出现的比较难的sql问题:20(触发器专题2)

    原文:在论坛中出现的比较难的sql问题:20(触发器专题2) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必要记录下 ...

  9. SQL Server2008分离数据库

    1.右击数据库 2.Tasks 3.点击Detach 4.选取Drop Connections-->点击确定 5.开启本地数据库默认存储路径C:\Program Files\Microsoft ...

  10. windows下搭建nginx负载均衡

    学习笔记,第一次记录避免忘记 首先介绍一下本地环境是windows2008 R2-64位. 1.  到nginx官网上下载最新稳定版的安装包,http://nginx.org/en/download. ...