消息队列是线程间通讯的手段:
import java.util.*
public class MsgQueue{
private Vector queue = null
public MsgQueue(){
queue = new Vector()
}
public synchronized void send(Object o)
{
queue.addElement(o)
}
public synchronized Object recv()
{
if(queue.size()==0)
return null
Object o = queue.firstElement()
queue.removeElementAt(0)//or queue[0] = null can also work
return o
}
}
因为java中是locked by object的所以添加synchronized 就可以用于线程同步锁定对象
可以作为多线程处理多任务的存放task的队列。他的client包括封装好的task类以及thread类
Java的多线程-线程间的通信2009-08-25 21:58
1. 线程的几种状态
线程有四种状态,任何一个线程肯定处于这四种状态中的一种:
1) 产生(New):线程对象已经产生,但尚未被启动,所以无法执行。如通过new产生了一个线程对象后没对它调用start()函数之前。
2) 可执行(Runnable):每个支持多线程的系统都有一个排程器,排程器会从线程池中选择一个线程并启动它。当一个线程处于可执行状态时,表示它可能正处于线程池中等待排排程器启动它;也可能它已正在执行。如执行了一个线程对象的start()方法后,线程就处于可执行状态,但显而易见的是此时线程不一定正在执行中。
3) 死亡(Dead):当一个线程正常结束,它便处于死亡状态。如一个线程的run()函数执行完毕后线程就进入死亡状态。
4) 停滞(Blocked):当一个线程处于停滞状态时,系统排程器就会忽略它,不对它进行排程。当处于停滞状态的线程重新回到可执行状态时,它有可能重新执行。如通过对一个线程调用wait()函数后,线程就进入停滞状态,只有当两次对该线程调用notify或notifyAll后它才能两次回到可执行状态。
2. class Thread下的常用函数函数
2.1 suspend()、resume()
1) 通过suspend()函数,可使线程进入停滞状态。通过suspend()使线程进入停滞状态后,除非收到resume()消息,否则该线程不会变回可执行状态。
2) 当调用suspend()函数后,线程不会释放它的“锁标志”。
例11:
class TestThreadMethod extends Thread{
public static int shareVar = 0
public TestThreadMethod(String name){
super(name)
}
public synchronized void run(){
if(shareVar==0){
for(int i=0i<5i++){
shareVar++
if(shareVar==5){
this.suspend() //(1)
}}}
else{
System.out.print(Thread.currentThread().getName())
System.out.println(" shareVar = " + shareVar)
this.resume() //(2)
}}
}
public class TestThread{
public static void main(String[] args){
TestThreadMethod t1 = new TestThreadMethod("t1")
TestThreadMethod t2 = new TestThreadMethod("t2")
t1.start() //(5)
//t1.start() //(3)
t2.start() //(4)
}}
class Element{int id
String name
Element(int a,String n){
id=aname=n
}
}
class SeqQueue{
int first,last,maxsize
Element queue[]
SeqQueue(int i){
maxsize=i
first=last=-1
queue=new Element[i]
}
public void clear(){//置空
first=last=-1
}
public boolean isEmpty(){//判空
if(first==-1)return true
else return false
}
public Element getFirst(){//取队列头元素
if(first==-1)return null
else return queue[first+1]
}
public boolean isFull(){//判满
if((last+1)%maxsize==first)return true
else return false
}
public boolean enQueue(Element e){//入队
if(this.isFull())return false
if(this.isEmpty())
first=last=0
else
last=(last+1)%maxsize
queue[last]=e
return true
}
public Element deQueue(){//出队
Element t=queue[first]
if(this.isEmpty())return null
if(first==last){
queue[first]=null
this.clear()
return t
}
queue[first]=null
first=(first+1)%maxsize
return t
}
public int getLength(){//队列长度
if(last>=first)return last-first+1
else return maxsize-(first-last)+1
}
public void display(){//打印所有元素
int i,j
for (i=first,j=0j<this.getLength()i=(i+1)%maxsize,j++)
System.out.println(queue[i].id)
}
}
java使用数据结构来实现FIFO先进先出的队列,实例如下:
/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest
import java.util.ArrayList
import java.util.Deque
import java.util.LinkedList
import java.util.List
/**
*
* @author Vicky.H
* @email [email protected]
*/
public class FIFOTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FIFO<A> fifo = new FIFOImpl<A>(5)
for (int i = 0 i < 20 i++) {
A a = new A("A:" + i)
A head = fifo.addLastSafe(a)
System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size())
}
System.out.println("---------------")
System.out.println("弹出数据")
List<A> polls = fifo.setMaxSize(3)
for (A a : polls) {
System.out.println("\thead:" + a)
}
System.out.println("剩余数据")
for (A a : fifo) {
System.out.println("\thead:" + a)
}
System.out.println(fifo.size())
}
}
interface FIFO<T> extends List<T>, Deque<T>, Cloneable, java.io.Serializable {
/**
* 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 *
*/
T addLastSafe(T addLast)
/**
* 弹出head,如果Size = 0返回null。而不同于pop抛出异常
* @return
*/
T pollSafe()
/**
* 获得最大保存
*
* @return
*/
int getMaxSize()
/**
* 设置最大存储范围
*
* @return 返回的是,因为改变了队列大小,导致弹出的head
*/
List<T> setMaxSize(int maxSize)
}
class FIFOImpl<T> extends LinkedList<T> implements FIFO<T> {
private int maxSize = Integer.MAX_VALUE
private final Object synObj = new Object()
public FIFOImpl() {
super()
}
public FIFOImpl(int maxSize) {
super()
this.maxSize = maxSize
}
@Override
public T addLastSafe(T addLast) {
synchronized (synObj) {
T head = null
while (size() >= maxSize) {
head = poll()
}
addLast(addLast)
return head
}
}
@Override
public T pollSafe() {
synchronized (synObj) {
return poll()
}
}
@Override
public List<T> setMaxSize(int maxSize) {
List<T> list = null
if (maxSize < this.maxSize) {
list = new ArrayList<T>()
synchronized (synObj) {
while (size() > maxSize) {
list.add(poll())
}
}
}
this.maxSize = maxSize
return list
}
@Override
public int getMaxSize() {
return this.maxSize
}
}
class A {
private String name
public A() {
}
public A(String name) {
this.name = name
}
public String getName() {
return name
}
public void setName(String name) {
this.name = name
}
@Override
public String toString() {
return "A{" + "name=" + name + '}'
}
}