就是突然想到以前看到过一道面试题似乎是用多线层打印出alibaba这个单词,然后网上搜不到,搜到了这个2个线程打印出1-100 ,就尝试了下记录一下
最初用syn和wait和notify方法组合的时候遇到了特别多的坑
因为wait 和notify会释放锁 然后我就直接想当然得
@Override
public void run() {
while (i <= 100) {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + "--------" + i++);
notifyAll();
wait();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
结果就报错IllegalMonitorStateException
这原因就是因为notify的时候并没有得到锁对象 前面我就特别迷 因为在syn里面就应该是有了锁对象了,然后我用this代替obj尝试 就陷入了死锁
@Override
public void run() {
while (i <= 100) {
synchronized (this) {
try {
System.out.println(Thread.currentThread().getName() + "--------" + i++);
notifyAll();
wait();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
!!!!!!!!!!!最后就突然觉悟的发现了 syn锁的是什么对象就应该用那个对象去调用wait和notify
我这里的this出现死锁是因为notify通知的是因为这个锁出现阻塞的线程(我以前一直以为通知的是所有的线程)
package zz;
/**
* @DATE: 2020/5/6
**/
public class ThreadDemo {
public static void main(String[] args)throws Exception {
new Thread(new MyRun(), "线程1").start();
new Thread(new MyRun(), "线程>>>2").start();
}
}
class MyRun implements Runnable {
static Object lock = new Object();
static volatile int i = 0;
@Override
public void run() {
while (i <= 100) {
synchronized (lock) {
try {
System.out.println(Thread.currentThread().getName() + "--------" + i++);
lock.notifyAll();
lock. wait();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
}
}
}
前面想到了syn是在多线程的情况下就是重量级锁,于是想到了能不能优化,分2种情况吧
1是不用锁 2是使用轻锁 或者是cas进行实现
package zz;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @DATE: 2020/5/6
**/
public class ThreadDemo {
static volatile int i = 0;
static volatile boolean flag = true;
public static void main(String[] args)throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (flag) {
System.out.println(Thread.currentThread().getName()+"----------"+i++);
flag=!flag;
}
}
}
}, "线程1").start();
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (!flag) {
System.out.println(Thread.currentThread().getName()+"+++"+i++);
flag=!flag;
}
}
}
}, "线程>>2").start();
}
}
class MyRun implements Runnable {
static Lock lock = new ReentrantLock();
static volatile int i = 0;
static volatile boolean flag = true;
@Override
public void run() {
while (i <= 100) {
if (flag)
System.out.println(i++);
flag=!flag;
}
}
}
package zz;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @DATE: 2020/5/6
* @author: zhanghong
**/
public class ThreadDemo {
static volatile int i = 0;
static volatile boolean flag = true;
public static void main(String[] args)throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (flag) {
System.out.println(Thread.currentThread().getName()+"=========="+i++);
flag=!flag;
}
}
}
}, "线程1").start();
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (!flag) {
System.out.println(Thread.currentThread().getName()+"=========="+i++);
flag=!flag;
}
}
}
}, "线程2").start();
}
}
class MyRun implements Runnable {
static Lock lock = new ReentrantLock();
static volatile int i = 0;
static volatile boolean flag = true;
@Override
public void run() {
while (i <= 100) {
if (flag)
System.out.println(i++);
flag=!flag;
}
}
}
package zz;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @DATE: 2020/5/6
* @author: zhanghong
**/
public class ThreadDemo {
static volatile int i = 0;
static volatile boolean flag = true;
public static void main(String[] args)throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (flag) {
System.out.println(Thread.currentThread().getName()+"----------"+i++);
flag=!flag;
}
}
}
}, "线程1").start();
new Thread(new Runnable() {
@Override
public void run() {
while (i<=100){
if (!flag) {
System.out.println(Thread.currentThread().getName()+"+++"+i++);
flag=!flag;
}
}
}
}, "线程>>2").start();
}
}
class MyRun implements Runnable {
static Lock lock = new ReentrantLock();
static volatile int i = 0;
static volatile boolean flag = true;
@Override
public void run() {
while (i <= 100) {
if (flag)
System.out.println(i++);
flag=!flag;
}
}
}
juc 还不会。。