Java thread(3)
线程间的调度策略
通常是选择优先级高的线程,但是若发生以下情况则终止线程的运行:
1 调用yield 让出对cpu的占用权。
2 调用sleep
3 线程由于I/O操作而受阻
4 更高优先级的线程出现
5 时间片用完
线程类的一些相关方法
isAlive()判断线程的死活、getPriority()得到线程的优先级、setPriority()设置线程的优先级、leep()方法使得线程休眠、yield方法放弃线程对cpu的使用权。
对于两个线程何时公用一个变量 何时私自拥有自己的变量的比较
当变量是类的成员变量的时候,这个变量被两个线程共享,如果是局部变量,比如定义在run方法中,这两个线程各自有自己的变量。
//情况一:
//两个线程 各自都有私有的变量 因为int变量声明在run方法中 结果会出来20个数
package com.javase.thread; public class threadTest2 { public static void main(String[]args){
Runnable r=new helloThread();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start(); }
} class helloThread implements Runnable{ public void run() {
int i=0;
while(true){
i++;
try {
Thread.sleep((long)Math.random()*2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if(i>10)
{break;}
System.out.println("the number is "+i);
}
} }
//情况二:
//int变量声明在类中 是类的成员变量 两个线程会共享着同一个公有的变量 结果会打印出来10个数字
package com.javase.thread; public class threadTest2 { public static void main(String[]args){
Runnable r=new helloThread();
Thread t1=new Thread(r);
Thread t2=new Thread(r);
t1.start();
t2.start(); }
} class helloThread implements Runnable{
int i=0; public void run() { while(true){
i++;
try {
Thread.sleep((long)Math.random()*2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} if(i>10)
{break;}
System.out.println("the number is "+i);
}
} }
package com.javase.thread;
public class threadTest2 {
public static void main(String[]args){
Runnable r1=new helloThread();
Runnable r2=new helloThread();
Thread t1=new Thread(r1);
Thread t2=new Thread(r2);
t1.start();
t2.start();
}
}
class helloThread implements Runnable{
int i=0;
public void run() {
while(true){
i++;
try {
Thread.sleep((long)Math.random()*2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(i>10)
{break;}
System.out.println("the number is "+i);
}
}
}
Java thread(3)的更多相关文章
- Java Thread 的 sleep() 和 wait() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. sleep ...
- Java Thread 的 run() 与 start() 的区别
Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别 1. ...
- Java Thread wait, notify and notifyAll Example
Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...
- java: Thread 和 runnable线程类
java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...
- Java Thread join() 的用法
Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...
- Java thread jargon
In Java thread topic, the task to be executed and the thread to drive the task are two concepts shou ...
- 性能分析之-- JAVA Thread Dump 分析综述
性能分析之-- JAVA Thread Dump 分析综述 一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...
- Java Thread线程控制
一.线程和进程 进程是处于运行中的程序,具有一定的独立能力,进程是系统进行资源分配和调度的一个独立单位. 进程特征: A.独立性:进程是系统中独立存在的实体,可以拥有自己独立的资源,每个进程都拥有自己 ...
- [译]Java Thread wait, notify和notifyAll示例
Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...
- [译]Java Thread Sleep示例
Java Thread Sleep示例 java.lang.Thread sleep(long millis)方法被用来暂停当前线程的执行,暂停时间由方法参数指定,单位为毫秒.注意参数不能为负数,否则 ...
随机推荐
- JavaScript基础4——省市联动
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Vue动态注册异步组件(非同一个工程的组件)
前言:最近在掘金逛的时候,无意中看到前滴滴前端架构黄轶大佬,看到了大佬分享的一篇博客滴滴 webapp 5.0 Vue 2.0 重构经验分享 ,对于其中第5个问题(异步加载的业务线组件,如何动态注册? ...
- 使用Tabulator遇到的问题
1.Tabulator好像是不支持ie,按照Tabulator文档引入,打开浏览器总是报缺少文件,换了谷歌果然好了. 2.编辑某一行的数据 代码: //Build Tabulator var tabl ...
- CentOS下性能监测工具 dstat
原文链接:http://www.bkjia.com/Linuxjc/935113.html 参考链接:https://linux.cn/article-3215-1.html,http://lhfli ...
- JVM垃圾回收算法图解
参考文档:https://www.toutiao.com/a6691966641242112516/ 垃圾搜集算法 标记-清除法 标记-清除算法采用从根集合(GC Roots)进行扫描,对存活的对象进 ...
- 【改】linux中分区的概念
1.目录和分区 区别:Linux的分区是物理上的概念,从物理上将存储空间分开:Linux的目录是逻辑上的概念,Linux的目录树实际上是一个分区之间的数据逻辑结构关系,不是物理结构: 联系:一个分区必 ...
- 【转】SIP协议 会话发起协议
转自:https://www.cnblogs.com/gardenofhu/p/7299963.html 会话发起协议(SIP)是VoIP技术中最常用的协议之一.它是一种应用层协议,与其他应用层协议协 ...
- 什么是shader?
一.什么是shader? shader是一段GLSL(openGL着色语言)小程序,运行在GPU(图形处理器),而非CPU使用GLSL语言编写,看上去像c或c++,但却是另外一种不同的语言.使用sha ...
- php内置函数分析之trim()
官方手册中: 类似函数还有两个:ltrim() 和 rtrim().分别处理字符串的左侧.右侧. trim()的具体实现位于:ext/standard/string.c /* {{{ proto st ...
- 关于pug的笔记
一.简介 Pug 是一款健壮.灵活.功能丰富的模板引擎,专门为 Node.js 平台开发.Pug 是由 Jade 改名而来,他可以帮助我们写html的时候更加的简单明了.安装.使用pug的过程打开cm ...