由于觉得自身对于JAVA中多线程技术的知识比较薄弱,所以对视频中多线程部分进行加强学习。
首先通过Thread对象完成简单的多线程程序编写,调试运行。然后主要练习线程继承Runnable方法进行程序设计。
class TestThread implements Runnable{
ABC abc;
public TestThread(ABC abc){
this.abc=abc;
}
对视频中卖100张票的程序进行独立编写。
利用synchronized关键字进行线程互斥与死锁问的处理。可以声明与方法或者变量之前的两种方法。
class Test {
Object o = new Object();
public void test() {
synchronized (o) {
// ............... } } }
public synchronized void st(){
System.out.println("... start.");
count++;
if(count>1){
available=true;
}
通过视频中存取姓名性别的例子的学习,掌握wait方法和notify方法对线程的操作机理。
class TestThread implements Runnable{
ABC abc;
public TestThread(ABC abc){
this.abc=abc;
}
class MyThread extends Thread {
Test t = new Test();
public void run() {
t.test();
System.out.println("Thread say:Hello,World!");
}
}
public class Test {
int x = 0;
public void test() {
if (x == 0)
try {
wait();
} catch (Exception e) {
}
}
public static void main(String[] args) throws Exception {
new MyThread().start();
}
}
最后对线程的生命周期进一步了解。