标签:long 状态 string 用户线程 抢票 小明 概率 xtend ati
获取当前线程对象,是一个静态方法,直接用类名调用即可
public class Thread_Demo01 {
public static void main(String[] args) {
A a = new A("李四"); //方式一
a.start();
// a.setName("张三"); //方式二
}
}
class A extends Thread{
public A(String name) {
super(name);
}
public void run() {
for(int i=0;i<10;i++) {
System.out.println(getName()+"-->"+i);
}
}
}
让当前线程休眠的指定毫秒,单数单位:毫秒,静态方法,可以直接通过类名调用
中断线程的休眠、等待状态,如果中断的线程正在休眠,则会抛出IntertuptedException
public class Thread_Demo02 {
public static void main(String[] args) {
SleepThread st = new SleepThread();
st.start();
for(int i=0;i<50;i++) {
System.out.println("张三正在学习-->"+i);
if(i==20) {
System.out.println("张三叫醒李四");
st.interrupt();
break;
}
}
}
}
class SleepThread extends Thread{
public void run() {
for(int i=0;i<10;i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("李四睡醒了,好好学习-->"+i);
}
}
}
设置优先级
获取优先级
优先级范围为:1 ~10
线程的优先级只能提高优先运行的概率
public class Thread_Demo03 {
public static void main(String[] args) {
Priority p1 = new Priority("A");
p1.setPriority(Thread.MAX_PRIORITY);
p1.start();
Priority p2 = new Priority("B");
p2.setPriority(Thread.MIN_PRIORITY);
p2.start();
Priority p3 = new Priority("C");
p3.setPriority(Thread.NORM_PRIORITY);
p3.start();
}
}
class Priority extends Thread{
public Priority(String name) {
super(name);
}
public void run() {
for(int i=0;i<5;i++) {
System.out.println(getName()+"的优先级为"+getPriority()+"-->"+i);
}
}
}
线程的礼让,礼让的时间不确定(不是绝对的礼让)
public class Thread_Demo04 {
public static void main(String[] args) {
YieldThread yt1 = new YieldThread();
yt1.start();
YieldThread yt2 = new YieldThread();
yt2.start();
}
}
class YieldThread extends Thread{
public void run() {
long time = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for(int i=0;i<10000;i++) {
if(getName().equals("Thread-0"))Thread.yield(); //如果是yt1执行时,实行礼让
sb.append("yield"+i);
}
time = System.currentTimeMillis() - time;
System.out.println(getName()+"耗时:"+time);
}
}
线程的插队,当前线程已经抢到cpu的占用权,让其他线程插队在自己前面执行,如果其他线程插队成功,则让其他线程先执行完再执行
public class Thread_Demo05 {
public static void main(String[] args) {
JoinThread jt = new JoinThread();
jt.start();
for(int i=100;i>0;i--) {
if(i==90) {
System.out.println("-------小红让小明插队---------");
try {
jt.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("小红抢票-->"+i);
}
}
}
class JoinThread extends Thread{
public void run() {
for(int i=30;i>0;i--) {
System.out.println("小明在抢票-->"+i);
}
}
}
true/false 默认false
public class Thread_Demo06 {
public static void main(String[] args) {
DaemonThread dt = new DaemonThread();
dt.setDaemon(true); //使dt线程变成守护线程
dt.start();
for(int i=0;i<100;i++) {
System.out.println("用户线程执行中-->"+i);
}
System.out.println("----------->"+Thread.currentThread().getName()+":"+System.currentTimeMillis());
}
}
class DaemonThread extends Thread{
public void run() {
while(true) {
System.out.println("守护线程执行中。。。。");
System.out.println(Thread.currentThread().getName()+":"+System.currentTimeMillis());
}
}
}
标签:long 状态 string 用户线程 抢票 小明 概率 xtend ati
原文地址:https://www.cnblogs.com/xiaokw/p/12678774.html