线程休眠实例
第一种方式:
public void Test(){
public static void main(String[] args){
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
try{
thread.sleep(3000); //先暂停3秒在启动
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thread.start();
}
}
=========================================================================
public class MyRunnable implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 100; i++) {
System.out.println("------MyRunnable");
}
}
}
第二种方式:
public class Test {
public static void main(String[] args) {
MyRunnable myRunnable = new MyRunnable();
Thread thread = new Thread(myRunnable);
thread.start();
}
}
=========================================================================
public class MyRunnable implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 100; i++) {
try {
Thread.currentThread().sleep(3000); //每执行一次暂停3秒
//Thread.currentThread() 是指获取当前运行的线程对象
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------MyRunnable");
}
}
}
第三种方式:
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
}
}
=========================================================================
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 100;i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("------Thread");
}
}
}
第四种方式
public class Test {
public static void main(String[] args) {
MyThread myThread = new MyThread();
try{
MyThread.sleep(3000);
}catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
myThread.start();
}
}
=========================================================================
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 100;i++) {
System.out.println("------Thread");
}
}
}
线程合并实例
public class Test {
public static void main(String[] args) {
JoinRunnable joinRunnable = new JoinRunnable();
Thread thread = new Thread(joinRunnable);
thread.start();
for (int i = 0; i < 100; i++) {
if(i == 10) {
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(i+"Test+++++++++++++++++");
}
}
}
=======================================================================
class JoinRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(i+"------------JoinRunnable");
}
}
}
线程礼让实例
public class Test{
public static void main(String[] args) {
YieldThread1 y1 = new YieldThread1();
y1.setName("YieldThread1");
YieldThread2 y2 = new YieldThread2();
y2.setName("YieldThread2");
y1.start();
y2.start();
}
}
class YieldThread1 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
if(i == 5) {
yield();
}
System.out.println(getName()+"------"+i);
}
}
}
class YieldThread2 extends Thread{
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 30; i++) {
System.out.println(getName()+"------"+i);
}
}
}
package day_4_13;
public class Test {
public static void main(String[] args) {
A a = new A();
Thread thread = new Thread(a);
thread.start();
System.out.println("现在线程的状态:" + thread.getState());
System.out.println("\n中断线程");
thread.interrupt();
System.out.println("线程现在是否处于中断状态(注意,只有线程启动了才有中断一说。\n如果连线程连启动都没有那还中断个锤锤) \n" + thread.isInterrupted());
System.out.println("\n现在线程的状态:" + thread.getState());
}
}
class A implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i =0;i<100;i++) {
System.out.println("奔跑吧 ! 皮卡丘");
}
}
}