线程间的调度策略

通常是选择优先级高的线程,但是若发生以下情况则终止线程的运行:
    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);
}
} }
//情况三://在第一种共享成员变量的情况下 如果生成两个runnable接口(helloThread对象)//则每个线程各自仍然会有自己的变量 不会发生冲突 两个helloThread对象 有各自的变量 结果会出来20个数字

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)的更多相关文章

  1. Java Thread 的 sleep() 和 wait() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别       1. sleep ...

  2. Java Thread 的 run() 与 start() 的区别

    Java Thread 的使用 Java Thread 的 run() 与 start() 的区别 Java Thread 的 sleep() 和 wait() 的区别             1. ...

  3. Java Thread wait, notify and notifyAll Example

    Java Thread wait, notify and notifyAll Example Java线程中的使用的wait,notify和nitifyAll方法示例. The Object clas ...

  4. java: Thread 和 runnable线程类

    java: Thread 和 runnable线程类 Java有2种实现线程的方法:Thread类,Runnable接口.(其实Thread本身就是Runnable的子类) Thread类,默认有ru ...

  5. Java Thread join() 的用法

    Java Thread中, join() 方法主要是让调用改方法的thread完成run方法里面的东西后, 在执行join()方法后面的代码.示例: class ThreadTesterA imple ...

  6. Java thread jargon

    In Java thread topic, the task to be executed and the thread to drive the task are two concepts shou ...

  7. 性能分析之-- JAVA Thread Dump 分析综述

    性能分析之-- JAVA Thread Dump 分析综述       一.Thread Dump介绍 1.1什么是Thread Dump? Thread Dump是非常有用的诊断Java应用问题的工 ...

  8. Java Thread线程控制

    一.线程和进程 进程是处于运行中的程序,具有一定的独立能力,进程是系统进行资源分配和调度的一个独立单位. 进程特征: A.独立性:进程是系统中独立存在的实体,可以拥有自己独立的资源,每个进程都拥有自己 ...

  9. [译]Java Thread wait, notify和notifyAll示例

    Java Thread wait, notify和notifyAll示例 Java上的Object类定义了三个final方法用于不同线程间关于某资源上的锁状态交互,这三个方法是:wait(), not ...

  10. [译]Java Thread Sleep示例

    Java Thread Sleep示例 java.lang.Thread sleep(long millis)方法被用来暂停当前线程的执行,暂停时间由方法参数指定,单位为毫秒.注意参数不能为负数,否则 ...

随机推荐

  1. 2.golang应用目录结构和GOPATH概念

    golang 语言有一个GOPATH的概念就是当前工作目录 [root@localhost golang_test]# tree . ├── bin │   └── hello ├── first.g ...

  2. expdp和impdp的应用-高版本通过dblink导入到低版本

    今天接到要进行数据库用户的部分数据迁移需求,需求如下 IMP.WO.INSA开头的表只要结构,不要数据 B.TEMP.TMP开头的表不用导 其他表需要导出数据和表结构,同时要求导出此用户下的所有其他对 ...

  3. P5444 [APIO2019]奇怪装置

    传送门 考虑求出最小的循环节 $G$ 使得 $t,t+G$ 得到的数对是一样的 由 $y \equiv t \mod B$ ,得到 $G$ 一定是 $B$ 的倍数,设 $zB=G$,则 $t,t+zB ...

  4. asp.net 关于SessionId

    原文:https://www.cnblogs.com/zhang1999/p/7278020.html 登陆页面使用Session存储验证码,导致会话产生SessionId,从而导致会话固定,登陆后用 ...

  5. Vue+Element-Ui 异步表单效验

    简单的效验 Form 组件提供了表单验证的功能,只需要通过 rules 属性传入约定的验证规则,并将 Form-Item 的 prop 属性设置为需校验的字段名 /* ruleForm :表单绑定的数 ...

  6. Python的五大数据类型的作用、定义方式、使用方法

    一.简述Python的五大数据类型的作用.定义方式.使用方法: 1. 数字类型int: 1.整形 作用:可以表示人的年龄,身份证号码,身高和体重等 定义方式:  weight = 130 print( ...

  7. vue中的scope

    在vue文件中的style标签上,有一个特殊的属性:scoped. 当一个style标签拥有scoped属性时,它的CSS样式就只能作用于当前的组件,也就是说,该样式只能适用于当前组件元素. 通过该属 ...

  8. linux中的一些常用命令

    shutdown -h now 现在马上关机 shutdown -r now 现在重新启动 reboot 现在重新启动 su - 如果当前是普通用户,则输入这条命令切换到管理员用户(root),如果要 ...

  9. "不能将值 NULL 插入列 'ID',表 列不允许有 Null 值."

    问题: "不能将值 NULL 插入列 'ID',表 列不允许有 Null 值." 原因: 在进行表创建的时候没有将主键自增字段添加标识. 在使用navicat进行表创建的时候一定要 ...

  10. Sass-@extend

    Sass 中的 @extend 是用来扩展选择器或占位符.比如: .error { border: 1px #f00; background-color: #fdd; } .error.intrusi ...