一、创建线程的两种方式
1 继承Runnable接口
public class ThreadImpRunnable implements Runnable
{
/**
* 线程运行时执行的方法
*/
public void run()
{
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class Test
{
/**
* 主线程,启动线程必须是start方法
*/
public static void main(String[] args)
{
ThreadImpRunnable tr = new ThreadImpRunnable();
Thread t = new Thread(tr);
t.start();
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
2 继承Thread类
public class ThreadExtends extends Thread
{
/**
* 线程运行时执行的方法
*/
public void run()
{
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class Test
{
/**
* 主线程,启动线程必须是start方法
*/
public static void main(String[] args)
{
ThreadExtends tr = new ThreadExtends();
tr.start();
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
二、线程的一些方法
1 sleep
public class SleepThread implements Runnable
{
/**
* 线程运行时执行的方法
*/
public void run()
{
try
{
// 该线程进入阻塞状态5秒
Thread.sleep(5000);
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
catch (InterruptedException e)
{
// 若调用该线程的interrupt方法,会报该异常,真实程序中可以关闭一些资源
e.printStackTrace();
}
}
}
public class SleepTest
{
/**
* 主线程
*/
public static void main(String[] args)
{
SleepThread tr = new SleepThread();
Thread t = new Thread(tr);
t.start();
for (int i = 0; i < 500; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
2 Join
public class JoinThread implements Runnable
{
public void run()
{
for (int i = 0; i < 100; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class JoinTest
{
public static void main(String[] args)
{
JoinThread jt = new JoinThread();
Thread t = new Thread(jt);
t.start();
try
{
// 调用该方法将当前线程(此处是主线程)合并到本线程中,执行完本线程,再执行当前线程
t.join();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
for (int i = 0; i < 100; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
3 yield
public class YieldThread extends Thread
{
public void run()
{
for (int i = 0; i < 100; i++)
{
System.out.println(Thread.currentThread().getName() + i);
if (i % 10 == 0)
{
// 当能被10整除时本线程让出优先级,让其他线程先执行一会,可看到t1_10下面紧接着t2的结果,同样t2_20下面紧接着t1的结果
yield();
}
}
}
}
public class YieldTest
{
public static void main(String[] args)
{
YieldThread yt = new YieldThread();
Thread t1 = new Thread(yt, "t1_");
t1.start();
Thread t2 = new Thread(yt, "t2_");
t2.start();
}
}
4 setPriority
public class PriorityThread extends Thread
{
public void run()
{
for (int i = 0; i < 100; i++)
{
System.out.println(Thread.currentThread().getName() + i);
}
}
}
public class PriorityTest
{
/**
* 线程优先级默认是5
*/
public static void main(String[] args)
{
int norm = Thread.NORM_PRIORITY; // 5
int max = Thread.MAX_PRIORITY; // 10
int min = Thread.MIN_PRIORITY; // 1
PriorityThread yt = new PriorityThread();
Thread t1 = new Thread(yt, "t1_");
t1.setPriority(norm + 3);
t1.start();
Thread t2 = new Thread(yt, "t2_");
t2.start();
}
}