computer 版 (精华区)

发信人: arbo (arbo), 信区: program
标  题: java 笔记8
发信站: 听涛站 (2001年09月17日10:50:33 星期一), 站内信件

发信人: airforce1 (五湖废人), 信区: Java
标  题: java 笔记8
发信站: BBS 水木清华站 (Sun Apr  2 14:26:00 2000)

还有一种实现线程的方式是派生thread类

class mythread extends thread{
  public void run{
...
}

有时,这种方式有些限制,例如在applet里没有办法用这种方法。
因为在applet里要extends aplet, java又不支持多继承。

用htread子类里可以直接sleep, 但是原来的runnable 借口是没有办法直接实现的
要在加入的thread类里才可以实现sleep.

线程同步
synchronized
为什么要用同步。
一个例子,对于一个堆栈,有push 和pop 方法
push(){
data[idx]=value;
idx++}
pop() {
value=data[idx];
idx--;
}
如果单线程做这些操作,没有问题,如果有一个以上的线程要有问题。
如果线程一push一个值,还没有idx累加,线程2做pop操作,就会取道了
不对的数据。

so , very object has a flag which can be thought of as a "lock flag"
synchronized allows interaction with the lock flag
原来的这个例子可以加一句
public void push(){
synchronized(this){
data[idx]=c;
idx++;
}}
pop()里也一样可以加一句,这样不会在两个语句序列出现交互操作了。
不过要在所有相关的内容里加这句话

解锁,synchronized()结束条件
1. released when the thread passes the end of the synchronized() code black
2.Automaticlall relaased when a break or exception is thrown
by the synchronized() code block

还有一种不放在函数体里的方式
public sychronized void push() ,直接放函数名前
不过前种效率要高一点。


线程的另外两个方法:
wait, notify
一个例子就是生产消费者问题,
生产出来了后就通知消费者,消费没有了就堵住
看程序吧,这段有操作系统理论课的人回觉得简单,不然要找书看看
package mod13;
public class Producer implements Runnable
{
        SyncStack theStack;

        public Producer(SyncStack s) {
                theStack = s;
        }

        public void run() {
                char c;
                for (int i = 0; i < 20; i++) {
                        c = (char)(Math.random() * 26 + 'A');
                        theStack.push(c);
                        System.out.println("Produced: " + c);
                        try {
                                Thread.sleep((int)(Math.random() * 100));
                        } catch (InterruptedException e) {
                                // ignore it..
                        }
                }
        }
}

package mod13;
public class SyncTest
{
        public static void main(String args[]) {
                SyncStack stack = new SyncStack();
                Runnable source = new Producer(stack);
                Runnable sink = new Consumer(stack);
                Thread t1 = new Thread(source);
                Thread t2 = new Thread(sink);
                t1.start();
                t2.start();
        }
}
package mod13;
public class SyncStack
{
        private int index = 0;
        private char [] buffer = new char[6];

        public synchronized char pop() {
                while (index == 0) {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // ignore it..
                        }
                }
                this.notify();
                index--;
                return buffer[index];
        }

        public synchronized void push(char c) {
                while (index == buffer.length) {
                        try {
                                this.wait();
                        } catch (InterruptedException e) {
                                // ignore it..
                        }
                }
                this.notify();
                buffer[index] = c;
                index++;
        }
}
package mod13;
public class Consumer implements Runnable
{
        SyncStack theStack;

        public Consumer(SyncStack s) {
                theStack = s;
        }

        public void run() {
                char c;
                for (int i = 0; i < 20; i++) {
                        c = theStack.pop();
                        System.out.println("
                        try {
                                Thread.sleep((int)(Math.random() * 1000));
                        } catch (InterruptedException e) {
                                // ignore it..
                        }
                }
        }
}

--
--
※ 来源:·听涛站 tingtao.dhs.org·[FROM: 匿名天使的家] 
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:1.540毫秒