Thread与Runnable的区别:
当一个线程运行时,另外一个线程可以直接通过interrupt()方法中断其运行状态。
继承自Thread的线程,彼此之间不共享线程的资源;
实现Runnable的接口,各个线程可以共享线程的资源。
看一个实例:
package com.yxf.threadandRunnable;
public class ThreadAndRunnable {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
MyThread t3 = new MyThread();
t1.start();
t2.start();
t3.start();
System.out.println("----------------------");
MyRunnable r1 = new MyRunnable();
new Thread(r1).start();
new Thread(r1).start();
new Thread(r1).start();
}
}
class MyThread extends Thread{
private int ticket = 5;
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i < 100;i++){
if(ticket > 0){
System.out.println("Thread " + Thread.currentThread().getId() +"-----Ticket_Thread : " + ticket--);
}
}
}
}
class MyRunnable implements Runnable{
private int ticket = 5;
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i < 100;i++){
if(ticket > 0){
System.out.println("Runnable " + Thread.currentThread().getId() + "-----Ticket_Runnable : " + ticket--);
}
}
}
}
输出结果如下:
Thread 8-----Ticket_Thread : 5
Thread 8-----Ticket_Thread : 4
Thread 8-----Ticket_Thread : 3
Thread 8-----Ticket_Thread : 2
Thread 8-----Ticket_Thread : 1
----------------------
Thread 10-----Ticket_Thread : 5
Thread 10-----Ticket_Thread : 4
Thread 10-----Ticket_Thread : 3
Thread 10-----Ticket_Thread : 2
Thread 10-----Ticket_Thread : 1
Thread 9-----Ticket_Thread : 5
Thread 9-----Ticket_Thread : 4
Thread 9-----Ticket_Thread : 3
Thread 9-----Ticket_Thread : 2
Thread 9-----Ticket_Thread : 1
Runnable 11-----Ticket_Runnable : 5
Runnable 11-----Ticket_Runnable : 4
Runnable 11-----Ticket_Runnable : 3
Runnable 11-----Ticket_Runnable : 2
Runnable 12-----Ticket_Runnable : 2
Runnable 11-----Ticket_Runnable : 1
继承自Thread的三个线程,他们独立的占有自己线程内的资源。实现Runnable的三个线程,他们共享了线程内的资源,但是也面临一个问题,资源的不同步。
线程的强制执行:
在线程操作中,可以使用join()方法让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等待此线程完成后才可以继续运行。
package com.yxf.threaddemos;
class MyRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0;i < 15;i++){
System.out.println("Thread name: " + Thread.currentThread().getName() + ", id = " +
Thread.currentThread().getId() + ", i = " + i);
}
}
}
public class ThreadDemos1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable,"my_thread");
thread.start();
/**
* 当i的值为0-10时,main和my_thread两个线程同时执行,当i > 10 时,my_thread线程被强制执行,直到执行完毕后才会执行main线程。
*/
for(int i = 0; i < 15;i++){
if(i > 10){
try{
thread.join();
}catch (Exception e){
}
}
System.out.println("Thread name: " + Thread.currentThread().getName() + ", id = " +
Thread.currentThread().getId() + ", i = " + i);
}
}
}
Output:
Thread name: main, id = 1, i = 0
Thread name: my_thread, id = 8, i = 0
Thread name: my_thread, id = 8, i = 1
Thread name: my_thread, id = 8, i = 2
Thread name: my_thread, id = 8, i = 3
Thread name: my_thread, id = 8, i = 4
Thread name: my_thread, id = 8, i = 5
Thread name: main, id = 1, i = 1
Thread name: my_thread, id = 8, i = 6
Thread name: main, id = 1, i = 2
Thread name: my_thread, id = 8, i = 7
Thread name: main, id = 1, i = 3
Thread name: my_thread, id = 8, i = 8
Thread name: main, id = 1, i = 4
Thread name: my_thread, id = 8, i = 9
Thread name: main, id = 1, i = 5
Thread name: my_thread, id = 8, i = 10
Thread name: main, id = 1, i = 6
Thread name: my_thread, id = 8, i = 11
Thread name: main, id = 1, i = 7
Thread name: my_thread, id = 8, i = 12
Thread name: main, id = 1, i = 8
Thread name: my_thread, id = 8, i = 13
Thread name: main, id = 1, i = 9
Thread name: my_thread, id = 8, i = 14
Thread name: main, id = 1, i = 10
Thread name: main, id = 1, i = 11
Thread name: main, id = 1, i = 12
Thread name: main, id = 1, i = 13
Thread name: main, id = 1, i = 14
线程的休眠:
在程序中允许一个线程进行暂时的休眠,直接使用Thread.sleep()方法即可实现休眠。
package com.yxf.threaddemos2;
class MyRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0; i < 5;i++){
try{
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
//每隔1s钟会打印一次
System.out.println(Thread.currentThread().getName() + ", i = " + i);
}
}
}
public class ThreadDemos2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnable runnable = new MyRunnable();
new Thread(runnable,"my_thread").start();
}
}
线程的中断:
当一个线程运行时,另外一个线程可以直接通过interrupt()方法中断其运行状态。
package com.yxf.threaddemos3;
class MyRunnable implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Enter run()");
try{
Thread.sleep(10000);
System.out.println("Finish sleep()");
}catch (Exception e){
System.out.println("Interrupt sleep()");
return;
}
System.out.println("Finish run()");
}
}
public class ThreadInterruptDemos {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyRunnable runnable = new MyRunnable();
Thread thread = new Thread(runnable,"my_thread");
thread.start();
try{
Thread.sleep(2000);
}catch (Exception e){
}
//这里将thread线程中断,会执行catch中的代码
thread.interrupt();
}
}
Output:
Enter run()
Interrupt sleep()