⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 writer.java

📁 一个电子白版程序,J2me程序,注意,使用EC开发,要安装WTK和其插件才能使用
💻 JAVA
字号:
import java.io.DataOutputStream;

import javax.microedition.io.SocketConnection;

import java.io.DataOutputStream;
import java.io.OutputStream;

/*
 * Created on 2006-2-18
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author blrc
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Writer extends Thread {

	private static final int bye = -100;
	
	ShareCanvas parent;
	SocketConnection currentConnection;
	OutputStream os;
	DataOutputStream dos;
	
	private int currentNum;
	boolean cancel;
	
	public Writer(ShareCanvas parent,SocketConnection currentConnection) {
		super();
		this.parent = parent;
		this.currentConnection = currentConnection;
		cancel = false;
		try
		{
			os = currentConnection.openOutputStream();
			dos = new DataOutputStream(os);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}		
	}
	
	public Writer(ShareCanvas parent,SocketConnection currentConnection,OutputStream os,DataOutputStream dos)
	{
		super();
		this.parent = parent;
		this.currentConnection = currentConnection;
		this.os = os;
		this.dos = dos;
	}
	
	public void run()
	{
		while (true)
		{
			a_wait();
			if (cancel) 
			{
				System.out.println("cancel = true and break");
				try
				{
					dos.writeInt(bye);
					dos.writeInt(bye);
					dos.flush();
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}				
				break;
			}
			for (int i = currentNum - 5; i < currentNum; i++)
			{
				Dimension dimension = (Dimension)parent.me.elementAt(i);
				try
				{
					dos.writeInt(dimension.x);
					dos.writeInt(dimension.y);
				}
				catch(Exception e)
				{
					e.printStackTrace();
				}				
			}	
			try{
				dos.writeInt(-1);
				dos.writeInt(-1);
				dos.flush();
				System.out.println("writer flush");
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		try
		{
			System.out.println("after break, Writer dos & os be close()");
			dos.close();
			os.close();
			Singal.WriterAlive = false;
			if (Singal.ReaderAlive == false)
			{
				System.out.println("at last, currentConnection be close()");
				currentConnection.close();
				parent.set_share(false);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		
		
	}
	
	public synchronized void a_wait()
	{
		 try
			{
		    	wait();			    	
			}
		    catch(Exception e)
			{
		    	e.printStackTrace();
			}
	}
	
	public synchronized void a_notifyAll()
	{
		notifyAll();
	}
	
	public void set_currentNum(int currentNum)
	{
		this.currentNum = currentNum;
	}
	
	public void cancel()
	{
		cancel = true;
		System.out.println("cancel = true, and call a_notifyAll()");
		a_notifyAll();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -