发表于: 2018-01-25 21:47:44

1 604


今天完成的事情:(一定要写非常细致的内容,比如说学会了盒子模型,了解了Margin) 

刚到任务8,因为到过年还有那么久,打算年前进入复盘,所以时间还比较充足,今天对基础部分缺失的知识进行学习.

还是多线程更深入的学习.


###25.04_多线程(两个线程间的通信)(掌握)
1.什么时候需要通信
    * 多个线程并发执行时, 在默认情况下CPU是随机切换线程的
    * 如果我们希望他们有规律的执行, 就可以使用通信, 例如每个线程执行一次打印
2.怎么通信
    * 如果希望线程等待, 就调用wait()
    * 如果希望唤醒等待的线程, 就调用notify();
    * 这两个方法必须在同步代码中执行, 并且使用同步锁对象来调用

package com;

/**
* @author Arike
* Create_at 2018/1/25 10:45
*/
public class test19 {
static int i=1;
   public static void main(String[] args) {
new Thread() {
@Override
           public void run() {
while (true) {
synchronized (test19.class) {
if (i != 1) {
try {
test19.class.wait();
                           } catch (InterruptedException e) {
e.printStackTrace();
                           }
}
System.out.print(Thread.currentThread().getName() + "     ");
                       System.out.print("");
                       System.out.print("");
                       System.out.print("");
                       System.out.println();
                       i = 2;
                       test19.class.notify();
                   }
}
}
}.start();
       
       new Thread() {
@Override
           public void run() {
while (true) {
synchronized (test19.class) {
if (i != 2) {
try {
test19.class.wait();
                           } catch (InterruptedException e) {
e.printStackTrace();
                           }
}
System.out.print(Thread.currentThread().getName() + "     ");
                       System.out.print("");
                       System.out.print("");
                       System.out.print("");
                       System.out.println();
                       i = 1;
                       test19.class.notify();
                   }
}
}
}.start();
   }
}

wait()和notify(),notifyAll()方法只能在synchronized()代码块或者synchronized方法中执行,并且 需要互相等待的线程需要使用同一个锁对象来等待和唤醒.

这个让我早上报了一个早上的异常,痛不欲生.

因为我全部使用的是匿名内部类的方式获取的线程以及方法,而我看的教程是使用的同步方法的方式,然后他是一个类,使用this来调用的wait和notify方法,让我下意识的认为这2个方法是由调用者来执行,进入了误区.

其实这2个方法只能由锁对象来调用,切记...并且notify只能唤醒同一个锁控制的其他线程,并不能唤醒非我锁类的线程.



###25.05_多线程(三个或三个以上间的线程通信)
多个线程通信的问题
    * notify()方法是随机唤醒一个线程
    * notifyAll()方法是唤醒所有线程
    * JDK5之前无法唤醒指定的一个线程
    * 如果多个线程之间通信, 需要使用notifyAll()通知所有线程, 用while来反复判断条件

package com;

/**
* @author Arike
* Create_at 2018/1/26 10:19
*/


   //2个线程以上的通讯需要使用while进行循环判断条件,并且使用notifyAll来唤醒所有线程,并且让他进行判断.
   //如果使用if的话,会在wait位置停下来, 唤醒之后会接着waite下面的代码执行,而使用while就会重新进入判断,只有条件满足了,才会往下走.
public class test21 {
static int i = 1;
   
   public static void main(String[] args) {
new Thread() {
@Override
           public void run() {
while (true) {
synchronized (test21.class) {
while (i != 1) {
try {
test21.class.wait();
                           } catch (InterruptedException e) {
e.printStackTrace();
                           }
}
System.out.print(Thread.currentThread().getName() + "     ");
                       System.out.print("");
                       System.out.print("");
                       System.out.print("");
                       System.out.println();
                       i = 2;
                       test21.class.notifyAll();
                   }
}
}
}.start();
       
       new Thread() {
@Override
           public void run() {
while (true) {
synchronized (test21.class) {
while (i != 2) {
try {
test21.class.wait();
                           } catch (InterruptedException e) {
e.printStackTrace();
                           }
}
System.out.print(Thread.currentThread().getName() + "     ");
                       System.out.print("");
                       System.out.print("");
                       System.out.print("");
                       System.out.println();
                       i = 3;
                       test21.class.notifyAll();
                   }
}
}
}.start();
       
       new Thread() {
@Override
           public void run() {
while (true) {
synchronized (test21.class) {
while (i != 3) {
try {
test21.class.wait();
                           } catch (InterruptedException e) {
e.printStackTrace();
                           }
}
System.out.print(Thread.currentThread().getName() + "     ");
                       System.out.print("");
                       System.out.print("");
                       System.out.print("");
                       System.out.println();
                       i = 1;
                       test21.class.notifyAll();
                   }
}
}
}.start();
   }
}



###25.06_多线程(JDK1.5的新特性互斥锁)(掌握)
1.同步
    * 使用ReentrantLock类的lock()和unlock()方法进行同步
2.通信
    * 使用ReentrantLock类的newCondition()方法可以获取Condition对象
    * 需要等待的时候使用Condition的await()方法, 唤醒的时候用signal()方法
    * 不同的线程使用不同的Condition, 这样就能区分唤醒的时候找哪个线程了


package com;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author Arike
* Create_at 2018/1/26 10:04
*/

/**
* 互斥锁
*/
public class test20 {
static int i = 1;
   public static void main(String[] args) {
final ReentrantLock rtl = new ReentrantLock();
       final Condition c1 = rtl.newCondition();
       final Condition c2 = rtl.newCondition();
       final Condition c3 = rtl.newCondition();
       new Thread(() -> {
while (true) {
rtl.lock();
               if (i != 1) {
try {
c1.await();
                   } catch (InterruptedException e) {
e.printStackTrace();
                   }
}
System.out.print("");
               System.out.print("");
               System.out.print("");
               System.out.print("\r\n");
               i = 2;
               c2.signal();
               rtl.unlock();
           }
}).start();
       
       new Thread(() -> {
while (true) {
rtl.lock();
               if (i != 2) {
try {
c2.await();
                   } catch (InterruptedException e) {
e.printStackTrace();
                   }
}
System.out.print("");
               System.out.print("");
               System.out.print("");
               System.out.print("\r\n");
               i = 3;
               c3.signal();
               rtl.unlock();
           }
}).start();
       
       new Thread(() -> {
while (true) {
rtl.lock();
               if (i != 3) {
try {
c3.await();
                   } catch (InterruptedException e) {
e.printStackTrace();
                   }
}
System.out.print("");
               System.out.print("");
               System.out.print("");
               System.out.print("\r\n");
               i = 1;
               c1.signal();
               rtl.unlock();
           }
}).start();
   }
}


还有很多理论性的东西,我也理解查阅了很久,就不写上日报了.


明天计划的事情:(一定要写非常细致的内容) 

进行RMI的学习.


遇到的问题:(遇到什么困难,怎么解决的) 

锁对象的认知还是不够深刻.


收获:(通过今天的学习,学到了什么知识)

多线程更深入的理解.


1


返回列表 返回列表
评论

    分享到