标签:多线程
使用方法:public static void sleep(long millis)throws InterruptedException{}
可见该方法有异常抛出,所以要进行异常的处理。
public class A implements Runnable {
private String name;
public A(String name) {
super();
this.name = name;
}
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(500);//休眠
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("运行:" + name);
}
}
}
测试一下:
public class ATest {
public static void main(String[] args) {
// TODO Auto-generated method stub
A mt1 = new A("线程1");
A mt2 = new A("线程2");
new Thread(mt1,"线程111").start();
System.out.println(Thread.currentThread().getName());
new Thread(mt2,"线程111").start();
}
}
public class MyThreadImplement implements Runnable {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(500);//休眠
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//得到当前的线程名称
System.out.println(Thread.currentThread().getName());
}
}
}
测试类:
public class MyThreadImplementTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
MyThreadImplement mt1 = new MyThreadImplement();
Thread m1 = new Thread(mt1,"线程1");
Thread m2 = new Thread(mt1,"线程2");
Thread m3 = new Thread(mt1,"线程3");
//使用setPriority()方法设置线程优先级
//传入的参数是 Thread类中的常量,分别表示优先级最高,正常,最低
m1.setPriority(Thread.MAX_PRIORITY);
m2.setPriority(Thread.NORM_PRIORITY);
m3.setPriority(Thread.MIN_PRIORITY);
m1.start();m2.start();m3.start();
}
}
调用interrupt()方法可以中断一个正在运行的线程!
看一个小例子:
public class TickThread implements Runnable {
//模拟卖票,共5张
private int tick = 5;
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 5; i++) {
if(this.tick>0){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("卖票:"+tick--);
}
}
}
}
测试类:
public class TickThreadTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
TickThread m = new TickThread();
new Thread(m,"A").start();
new Thread(m,"B").start();
new Thread(m,"C").start();
}
}
通过运行结果发现出现了异常。原因是代码中加入了休眠的操作,线程A正在休眠,线程B运行起来卖掉了当前线程A的票,A休眠结束后,继续卖票,出现了上面的情况。下面通过加入同步解决这个问题。
所谓同步,多个操作在同一个时间段只能有一个在进行,A线程在运行后,就不会
1)同步代码块实现同步
修改后的代码:
public class TickThread implements Runnable {
private int tick = 5;
@Override
public void run() {
// TODO Auto-generated method stub
//synchronized声明同步代码块
synchronized (this) {
for (int i = 0; i < 5; i++) {
if(this.tick>0){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行"+tick--);
}
}
}
}
}
也可以把要同步的代码写成一个方法
修改后的代码:
public class TickThread implements Runnable {
private int tick = 5;
@Override
public void run() {
// TODO Auto-generated method stub
//synchronized声明同步代码块
fun();
}
public synchronized void fun(){
for (int i = 0; i < 5; i++) {
if(this.tick>0){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+"运行"+tick--);
}
}
}
}
多个线程共享一个数据时需要进行同步,但过多的同步会产生死锁;
标签:多线程
原文地址:http://blog.csdn.net/u012437355/article/details/45980213