使用多线程,最重要的就是安全问题,何为安全问题?
public class ThreadTest1 {
public static void main(String[] args) {
MyRunnable runnable = new MyRunnable();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
new Thread(runnable).start();
}
}
class MyRunnable implements Runnable{
private int i = 100;
public void run() {
while(true){
if(i > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i--);
}
}
}
}
执行结果:
....
8
7
6
5
4
3
2
1
0
-1
-2
可以看出,执行结果出了问题,出现了0,-1,-2等非法数据,这是因为:
这就是使用多线程产生的安全问题。
产生的原因是:
多线程操作共享数据。
解决方法:对操作共享数据的代码片段进行同步。使用关键字synchronized.
第一:同步代码块
语法:
synchronized(对象){
//要同步的代码块
}
synchronzied上锁必须有个对象,同一个对象锁才能保证上锁是对多个线程之间有作用的。
对上述有安全问题的代码进行改造:
class MyRunnable implements Runnable{
private int i = 100;
public void run() {
while(true){
synchronized(this){ //这个this代表当前对象,这里换成其他对象也可以的。
if(i > 0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i--);
}
}
}
}
}
这样就不会出问题了。
第二种:同步方法
语法: public synchronized void add().。用synchronized关键字对方法进行修饰,那么此方法就成为了同步方法。
class MyRunnable implements Runnable{
private int i = 100;
public void run() {
while(true){
cutDown();
}
}
public synchronized void cutDown(){
if(i>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i--);
}
}
}
值得注意的是,我们说了,synchronzied需要一个对象锁,而对方法上锁并没有看到这个锁在哪里,其实对方法上锁默认为this锁。
第三种:对静态方法上锁
public static synchronized void cutDown(){
if(i>0){
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(i--);
}
}
静态方法的锁,是他所在类的class对象,比如说上述实例,他的锁就是MyRunnable.class对象,如果有静态代码块需要和他同步,那么synchronized(类.class){}来同步,否则不起作用。