线程1
    与线程相关的概念
    线程与进程的区别
    线程创建策略
    线程组
    
    线程创建策略
        并发应用中一般有两种不同的线程创建策略
        1直接控制线程的创建和管理
        2将线程的管理从应用程序中抽象出来作为执行器,应用程序将任务传递给执行器,由执行器负责执行。
线程2
    在线程中实现自己的代码逻辑
    
    有3种方式
        从Thread类继承
        实现runnable接口
        使用方法引用到一个无参数无返回值的方法
        
    将参数传入线程中
        使用线程类属性的set方法
        使用线程类的构造方法传值

package java20180203_1;

class Thread1 extends Thread {
private int num;
public void setNum(int num) {
this.num = num;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread1:" + i+","+num);
}
} } class Thread2 implements Runnable {
private int num;
public Thread2(int num) {
this.num = num;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread2:" + i+","+num);
}
}
} public class ThreadDemo { static void print(){
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread4:" + i);
}
}
public static void main(String[] args) throws Exception {
/**
* 线程组,将线程加入到线程组
*/
Thread t1 = new Thread();
t1.start(); ThreadGroup tg1 = new ThreadGroup("ThreadGroup");
Thread t2 = new Thread(tg1, "t2"); /**
* 从Thread类继承,这是子线程1
*/
Thread1 th1 = new Thread1();
//使用线程类set属性进行传值
th1.setNum(200);
th1.start(); /**
* 实现runnable接口,这是子线程2
*/
//使用构造方法传值
Runnable r2 = new Thread2(300);
Thread t3 = new Thread(r2);
t3.start(); /**
* 用lambda表达式来实现相同的功能,这是子线程3
*/
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread3:" + i);
}
}).start(); /**
* 方法引用来创建线程
*/
new Thread(ThreadDemo::print).start(); /**
* 这是main线程的循环
* 总共有4个线程,一个是main主线程的循环,其余3个是子线程的循环,有3个start,它们是并行的,可以从结果中看到。
* 分发给不同的处理器来执行
*/
for (int i = 0; i < 100; i++) {
Thread.sleep(500);
System.out.println("main:" + i);
}
}
}

线程3
    获取线程的信息
    线程优先级
    线程串行化
    线程休眠
    
    获取线程的信息
        currentThread()
        getName
    优先级
        默认优先级是5,子默认与父优先级相同,共10个,从1到10。
    串行化
        若一个线程运行的过程中要用到另一个线程的结果,则可以进行线程的串行化处理
        join()方法
    休眠
        sleep()方法,暂停时间不精确

线程4
    线程的状态
    线程中断
    线程停止
    守护线程
    
    
    线程的状态
        可以使用getState()来获取,有New,Runnable,Blocked,Waiting,ThreadWaiting,Dead这6种状态

线程停止
        开发中使线程停止的方式不是使用其stop(),已经不推荐使用了
        而是使用标识量来停止线程

package java20180205;

public class InterruptDemo {

    public static void main(String[] args) throws Exception {
Thread t1=new Thread(()->{
for (int i = 0; i < 20; i++) {
System.out.println(i);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start();
for (int i = 0; i < 20; i++) {
System.out.println("main:"+i);
Thread.sleep(300);
}
t1.interrupt();
/**
* 一个是false,一个是true
*/
// System.out.println(t1.interrupted());
// System.out.println(t1.isInterrupted());
} }
package java20180205;

class Thread4 implements Runnable{

    private boolean stop=false;
public void setStop(boolean stop) {
this.stop = stop;
} @Override
public void run() {
for (int i = 0;; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (stop) {
break;
}
System.out.println(i);
}
}
} public class StopDemo { public static void main(String[] args) throws Exception {
// Thread4 t4=new Thread4();
// new Thread(t4).start();
// Thread.sleep(4000);
// t4.setStop(true); Thread4 t4=new Thread4();
Thread t5=new Thread(t4);
t5.setDaemon(true);
t5.start();
Thread.sleep(4000); }
}

线程5
    多线程访问共享数据的问题
    java内存模型
        
    案例
        使用多个线程调用一个账号实例中的取款方法
    java内存模型
        线程安全术语

线程6
    对象监视器
    synchronized关键字
    volatile变量
    
    对象监视器
        临界区,监视器上加锁
    
    synchronized关键字
        用于声明临界区
        两种方法:
            synchronized方法
                实例方法:锁加在实例上
                静态方法:锁加在类上
            synchronized块
        
线程7    05:29
    生产者消费者模型
    wait与sleep的区别
    
    生产者消费者模型
        线程间通信的另一种方式,是一个非常经典的模型

java_线程的更多相关文章

  1. java_线程-锁

    package com.demo.test3; import java.util.concurrent.CountDownLatch; /** * @author QQ: 1236897 * */ / ...

  2. java_线程安全-service

    package com.demo.test; import java.util.Collections; import java.util.HashMap; import java.util.Map; ...

  3. java_线程的几种状态

    java thread的运行周期中, 有几种状态, 在 java.lang.Thread.State 中有详细定义和说明: NEW 状态是指线程刚创建, 尚未启动 RUNNABLE 状态是线程正在正常 ...

  4. java_线程的通信

    线程的通信共有三个方法: wait()运行时阻塞,释放锁 notify()唤醒阻塞线程 notifll()唤醒全部阻塞线程 public class ThreadTest01 { public sta ...

  5. java_线程分类

    线程分为守护线程和用户线程,如java虚拟机的回收机制就是守护线程,线程开始运行它就启动,线程结束它就结束 用户线程变守护线程:Thread(线程).setDaemon(true)

  6. java_线程优先级

    线程优先级分为三个等级: MAX_PIORITY:10  优先 MIN_PRIORITY:1 NORM_PRIORITY:5  默认 getPriority:获取优先级 setPriority:设置优 ...

  7. java_线程类的基本功能

    Thread类是实现了Runnable接口 其方法有: start()开始:开始线程 run()跑:线程内容 currentThread()现在的线程:返回当前线程 getName():获取线程名 s ...

  8. java_线程创建的两种方法

    线程创建的方法有两种: 一 继承Thread类: public class ThreadTest { public static void main(String[] args) { //4)在mai ...

  9. java_线程、同步、线程池

    线程 Java使用 java.lang.Thread 类代表线程,所有的线程对象都必须是Thread类或其子类的实例 Thread类常用方法 构造方法 public Thread():分配一个新的线程 ...

随机推荐

  1. 【SMTP】常见错误码

    '* 邮件服务返回代码含义 '* 500 格式错误,命令不可识别(此错误也包括命令行过长) '* 501 参数格式错误 '* 502 命令不可实现 '* 503 错误的命令序列 '* 504 命令参数 ...

  2. Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误

    Eclispe创建maven工程缺失web.xml报web.xml is missing and <failOnMissingWebXml> is set to true的错误,一看,还缺 ...

  3. windows 多用户登陆设置

    在 “运行”中输入gpedit.msc打开“本地组策略编辑器” | 计算机配置 | 管理模板 | windows 组件 | 远程桌面服务 | 远程桌面会话主机 | 连接 | 限制连接的数量 | 编辑 ...

  4. ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD

    设置了Nested attributes后,你可以通过父记录来更新/新建/删除关联记录. 使用: #accepts_nested_attributes_for class method. 例如: cl ...

  5. Ubuntu环境下,项目出现:Call to undefined function curl_init() 提示

    原因: 没有开启curl扩展 安装或者开启扩展 ubuntu 执行安装Curl的扩展 sudo apt-get install -y php-curl 同: mysql,redis,curl,等. 摘 ...

  6. edu30F. Forbidden Indices

    题意:给你一个字符串s有一些位置被ban了,字符串t的价值是|t|*t在s中出现次数而且终点没有被ban.问你最大的价值是多少 题解:很明显t是s子串,建个sam,对于sam中每个位置,我们需要删除中 ...

  7. PAT 1058 A+B in Hogwarts

    1058 A+B in Hogwarts (20 分)   If you are a fan of Harry Potter, you would know the world of magic ha ...

  8. QLineSeries QChartView 生成折线

    效果图 // 创建折线上点的序列 QLineSeries *splineSeries = new QLineSeries(); //QSplineSeries *splineSeries = new ...

  9. linux 程序无缘无故推出 没有core文件 broken pipe Resource temporarily unavailable

    问题 1. linux socket 服务端程序 无缘无故退出 . 2. 客户端大量访问服务端后,出现  Resource temporarily unavailable错误 问题分析: 1. 是否有 ...

  10. Python中文问题

    读取数据库中文是?? 解决如下 一.python2版本需要在 文件的开头要加上编码设置来说明文件的编码  python3版本以上不需要 #encoding=utf-8 二.在连接数据的连接参数里加上字 ...