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

📄 server.java

📁 java thread using bank java thread using bank java thread using bank java thread using bank java
💻 JAVA
字号:
import java.io.DataInputStream;import java.io.PrintStream;import java.net.ServerSocket;/* * To change this template, choose Tools | Templates * and open the template in the editor. */import java.net.Socket;/** * * @author root */public class Server {    public static void main(String[] args) throws Exception    {        new Listener();    }}interface Intf{    public String withdraw(int amt);    public String deposit(int amt);    public String balance();}class Listener extends Thread implements Intf{    public Bank B;    public Listener()    {     	B=new Bank();     	B.login(12345,(short)5555);        start();    }    public String withdraw(int amt)    {    	String s="";    	if(B.lock){    		while(B.lock);}    	B.lock=true;   		s=B.withdraw(amt);   		B.lock=false;   		return s;    }    public String balance()    {    	return B.getBal();    }    public String deposit(int amt)    {    	String s="";    	if(B.lock){    		while(B.lock);}    	B.lock=true;   		s=B.deposit(amt);   		B.lock=false;   		return s;    }    public void run()    {       try{           ServerSocket ss=new ServerSocket(5555);           while(true){           new Reader(this,ss.accept());           }       }catch(Exception e){}    }}class Reader extends Thread{    Socket S;    Intf N;    public Reader(Intf n,Socket s)    {        S=s;        N=n;        start();    }    public void run()    {        try{            PrintStream out=new PrintStream(S.getOutputStream());            DataInputStream in=new DataInputStream(S.getInputStream());            while(true){            int opt=Integer.parseInt(in.readLine());            if(opt==1)            {            	int amt=Integer.parseInt(in.readLine());                out.println(N.deposit(amt));            }            else if(opt==2)            {            	int amt=Integer.parseInt(in.readLine());	            out.println(N.withdraw(amt));            }            else if(opt==3)            {	            out.println(N.balance());            }            else            	break;            }            in.close();            out.close();        }catch(Exception e){}    }}

⌨️ 快捷键说明

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