转载请注明出处:线程及线程锁synchronized 、volatile_Mr_Leixiansheng的博客-CSDN博客
1、关键 synchronized
public class Test implements Runnable{
Timer timer = new Timer();
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
Thread t1 = new Thread(test);
Thread t2 = new Thread(test);
t1.setName("t1");
t2.setName("t2");
t1.start();
t2.start();
}
@Override
public void run() {
timer.add(Thread.currentThread().getName());
}
}
class Timer {
private static int num = 0;
public synchronized void add(String name) {
num++;
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
System.out.println(name +",你是第"+ num +"个线程" );
}
}
class Test2 implements Runnable{
private Object lockObj = new Object();
@Override
public void run() {
synchronized (lockObj) {
while("xx不满足条件"){
//条件不满足,将此线程放入wait set
lockObj.wait();
}
//执行完任务后,唤醒lockObj对象中等待的线程
lockObj.notifyAll();
}
}
}
package com.leixiansheng;
public class ThreadTest {
public static void main(String[] args) {
MyThread mt = new MyThread();
Thread t1 = new Thread(mt, "线程1");
Thread t2 = new Thread(mt, "线程2");
Thread t3 = new Thread(mt, "线程3");
t1.start();
t2.start();
t3.start();
}
}
class MyThread implements Runnable {
@Override
public void run() {
synchronized (this) {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName()+ " synchronized loop " + i);
}
}
}
}
结果:
线程1 synchronized loop 0
线程1 synchronized loop 1
线程1 synchronized loop 2
线程1 synchronized loop 3
线程1 synchronized loop 4
线程3 synchronized loop 0
线程3 synchronized loop 1
线程3 synchronized loop 2
线程3 synchronized loop 3
线程3 synchronized loop 4
线程2 synchronized loop 0
线程2 synchronized loop 1
线程2 synchronized loop 2
线程2 synchronized loop 3
线程2 synchronized loop 4
2、volatile:简易解释:关闭线程标识符,保证线程可见,可以操作线程
package com.leixiansheng;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test extends Thread{
@Override
public void run() {
Test2 test2 = new Test2();
Thread testTast = new Thread(test2, "Mrs");
testTast.start();
try {
Thread.sleep(1000); //线程睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
test2.isRunning = false; //结束Test2()线程
}
public static void main(String[] args) throws InterruptedException {
new Test().start();
}
}
class Test2 implements Runnable{
/**
* volatile 保证可见性
*/
volatile boolean isRunning = true;
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"演出开始");
int count = 0;
while(isRunning) {
System.out.println(Thread.currentThread().getName()+"上场次数:"+ (++count));
try {
Thread.sleep(50); //线程睡眠
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName()+"演出结束");
}
}
没有volatile修饰的标识符,线程就不能停止
package com.leixiansheng;
public class ThreadTest {
private static volatile boolean isRunning = true;
private static int num=0;
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
while(isRunning) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(num);
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
while(isRunning){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
num++;
}
}
}).start();
}
}