Sleep休眠
public class TestSleep implements Runnable {
private int ticketNum = 30 ;
@Override
public void run ( ) {
while ( true ) {
if ( ticketNum<= 0 ) {
break ;
}
try {
Thread. sleep ( 200 ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
System. out. println ( Thread. currentThread ( ) . getName ( ) + "-->拿到了第" + ticketNum-- + "张票" ) ;
}
}
public static void main ( String[ ] args) {
TestThread4 ticket = new TestThread4 ( ) ;
new Thread ( ticket, "mlxg" ) . start ( ) ;
new Thread ( ticket, "zsmj" ) . start ( ) ;
new Thread ( ticket, "nmsl" ) . start ( ) ;
}
}
import java. text. SimpleDateFormat;
import java. util. Date;
public class TestSleep2 {
public static void main ( String[ ] args) {
Date nowtime = new Date ( System. currentTimeMillis ( ) ) ;
while ( true ) {
try {
Thread. sleep ( 1000 ) ;
System. out. println ( new SimpleDateFormat ( "HH:mm:ss" ) . format ( nowtime) ) ;
nowtime = new Date ( System. currentTimeMillis ( ) ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
}
public static void tenDown ( ) throws InterruptedException {
int num = 10 ;
while ( true ) {
Thread. sleep ( 1000 ) ;
System. out. println ( num-- ) ;
if ( num<= 0 ) {
System. out. println ( "新年快乐~~" ) ;
break ;
}
}
}
}
Stop停止
package com. study. state;
public class TestStop implements Runnable {
private boolean flag = true ;
@Override
public void run ( ) {
int i = 0 ;
while ( flag) {
System. out. println ( "run...Thread" + i++ ) ;
}
}
private void stop ( ) {
this . flag= false ;
}
public static void main ( String[ ] args) {
TestStop testStop = new TestStop ( ) ;
new Thread ( testStop) . start ( ) ;
for ( int i = 0 ; i < 1000 ; i++ ) {
if ( i== 900 ) {
testStop. stop ( ) ;
System. out. println ( "该线程该停止了" ) ;
}
}
}
}
yield礼让
public class Testyield {
public static void main ( String[ ] args) {
MyYield myYield = new MyYield ( ) ;
new Thread ( myYield, "a" ) . start ( ) ;
new Thread ( myYield, "b" ) . start ( ) ;
}
}
class MyYield implements Runnable {
@Override
public void run ( ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "线程开始" ) ;
Thread. yield ( ) ;
System. out. println ( Thread. currentThread ( ) . getName ( ) + "线程结束" ) ;
}
}
Join加入
public static void main ( String[ ] args) {
TestJoin testJoin = new TestJoin ( ) ;
Thread thread = new Thread ( testJoin) ;
thread. start ( ) ;
for ( int i = 0 ; i < 1000 ; i++ ) {
if ( i== 100 ) {
try {
thread. join ( ) ;
} catch ( InterruptedException e) {
e. printStackTrace ( ) ;
}
}
System. out. println ( "main" + i) ;
}
}
线程优先级
public class TestPriority {
public static void main ( String[ ] args) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "-->" + Thread. currentThread ( ) . getPriority ( ) ) ;
MyPriority myPriority = new MyPriority ( ) ;
Thread t1 = new Thread ( myPriority) ;
Thread t2 = new Thread ( myPriority) ;
Thread t3 = new Thread ( myPriority) ;
Thread t4 = new Thread ( myPriority) ;
Thread t5 = new Thread ( myPriority) ;
t1. start ( ) ;
t2. setPriority ( 1 ) ;
t2. start ( ) ;
t3. setPriority ( 4 ) ;
t3. start ( ) ;
t4. setPriority ( Thread. MAX_PRIORITY) ;
t4. start ( ) ;
t5. setPriority ( 9 ) ;
t5. start ( ) ;
}
}
class MyPriority implements Runnable {
@Override
public void run ( ) {
System. out. println ( Thread. currentThread ( ) . getName ( ) + "-->" + Thread. currentThread ( ) . getPriority ( ) ) ;
}
}
守护线程
public class testDemon {
public static void main ( String[ ] args) {
God god = new God ( ) ;
You you = new You ( ) ;
Thread thread = new Thread ( god) ;
thread. setDaemon ( true ) ;
thread. start ( ) ;
new Thread ( you) . start ( ) ;
}
}
class God implements Runnable {
@Override
public void run ( ) {
while ( true ) {
System. out. println ( "上帝照顾你" ) ;
}
}
}
class You implements Runnable {
@Override
public void run ( ) {
for ( int i = 0 ; i < 36500 ; i++ ) {
System. out. println ( "活着-->" ) ;
if ( i == 36499 ) {
System. out. println ( "你安详的去了" ) ;
break ;
}
}
}
}