一、创建线程有两种方法:分别为继承Thread类方式(Thread类已实现Runable接口)和实现Runable接口方式。
继承Thread类方式通过重写Thread类中的方法run(),以实现用户所需要的功能,实例化自定义的Thread类,使用start()方法启动线程。
Public class 类名 extends Thread{
Public void run(){
//需要进行的操作
}
}
程序范例:
package threadtest;
public class threaddemo extends Thread{
private String name = "";
public threaddemo(String name)
{
this.name = name;
}
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println(name);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String []args)
{
Thread t1=new threaddemo("zs");
Thread t2=new threaddemo("ls");
t1.start();
t2.start();
}
}
如果使用继承Thread类实现线程类,将导致无法继承其他的父类,因此可以通过使用Runnable接口实现线程类。
Runnable接口同样定义了run()方法,实现该接口的同时,必须实现接口中的run()方法。当使用Thread()方法创建线程对象时,需要为该方法传递一个实现了Runnable接口的类对象,这样创建的线程将调用那个实现了Runnable接口的类对象中的run()方法作为其运行代码。
Public class 类名 implements Runnable{
Public void run(){
//需要进行的操作
}
}
程序范例:
package threadtest;
class test implements Runnable{
String s = "";
int time = 0;
public void run (){
for (int i=0;i<10;i++)
{
try {
Thread.sleep(time);
} catch (InterruptedException e) {
//Thread.interrupted();
}
System.out.print(s);
}
}
}
public class TestThread2 {
public static void main(String[] args) {
test t1 = new test();
test t2 = new test();
t1.s = "a";
t1.time = 100;
t2.s = "b";
t2.time = 200;
Thread a = new Thread(t1);
a.start();
Thread b = new Thread(t2);
b.start();
}
}
二、怎么样使线程停止:
使用stop()方法可以停止线程,但是该方法会造成系统进入不安全的状态,因此在JDK 1.1之后就取消了这个方法。现在提倡在run()方法中使用布尔型标记控制循环停止。
例如:
public class interruptTest implement Runnable{
private boolean isContinue=false;
public void run(){
while(true){
//需要进行的操作
if(isContinue)
break;
}
}
public void setContinue(){
this.isContinue=true;
}
}
三、设置线程的优先级可以通过如下语法:
setPriority(int newPriority);
New priority:线程的优先级。可选择Thread.MIN_PRIORITY(常数1)、Thread.MAX_PRIORITY(常数10)、Thread.NORM_PRIORITY(常数5),优先级范围是1-10,默认值为5。通过设置优先级,先启动的线程不一定先运行,虚拟机会考虑线程的优先级。
四、线程的加入方法
调度方法中有一个是join()方法,它使一个线程2跟在当前运行线程1后面运行,当加入的线程2执行完成后,再执行线程1。
public class joinThread {
public static void main(String[] args) {
myThread1 m1=new myThread1();
myThread2 m2=new myThread2();
myThread3 m3=new myThread3();
m2.mt=m1;
m1.start();
m2.start();
m3.start();
}
}
class myThread1 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("me");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("exception"+e);
}
}
}
}
class myThread2 extends Thread
{
public myThread1 mt;
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("you");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("exception"+e);
}
if(i==5)
{
try {
mt.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("exception"+e);
}
}
}
}
}
class myThread3 extends Thread
{
public void run()
{
for(int i=0;i<10;i++)
{
System.out.println("he");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("exception"+e);
}
}
}
}
从运行结果可以看到当打印第5个“you”后,然后程序中“me“和”he“交替出现,最后是剩下的”you“。
可以看出,join()方法就是让当前运行的线程停止运行,开始运行加入的线程。和这2个无关的线程,继续按它的顺序运行,如myThread3线程。
五、中断线程
中断是指示一个线程停止当前正在做的事情,转而去做一些其他的是。一个线程通过调用Thread对象的interrupt()方法发出一个中断信号给要中断的线程。要使中断机制正确的工作,被中断的线程必须支持自身的中断。
public class interruptThread {
public static void main(String[] args) throws InterruptedException {
String []mes={"消息1","消息2","消息3","消息4"};
for(int i=0;i<mes.length;i++)
{
try{
Thread.sleep(2000);
}catch(InterruptedException e)
{
return;
}
System.out.println(mes[i]);
}
}
}

401

被折叠的 条评论
为什么被折叠?



