我可迟宏以给你些 提示
建立个 TCP 套接字 监听在 某 个端口,当 接受到某IP来的数据猛旦芹后
保存该 IP 然后 自动 对该 套接枝毕字的 输出流 内 写入 一段回复内容到指定IP~
import java.math.BigDecimalpublic class JavaCourse {
private String sName//学生姓肆拆名
private String sId //学号
private int testGrade //测试成绩
private int regularGrade//平时成绩
private int endOfTermGrade//期末成绩
private int totalGrade //总成绩
public final double TR= 0.3
public final double RR= 0.3
public final double ER= 0.4
public JavaCourse() {
}
public JavaCourse(String sName, String sId, int testGrade, int regularGrade, int endOfTermGrade) {
this.sName = sName
this.sId = sId
this.testGrade = testGrade
this.regularGrade = regularGrade
this.endOfTermGrade = endOfTermGrade
}
public String getsName() {
return sName
}
public void setsName(String sName) {
this.sName = sName
}
public String getsId() {
return sId
}
public void setsId(String sId) {
this.sId = sId
}
public int getTestGrade() {
return testGrade
}
public void setTestGrade(int testGrade) {
this.testGrade = testGrade
}
public int getRegularGrade() {
return regularGrade
}
public void setRegularGrade(int regularGrade) {
this.regularGrade = regularGrade
}
public int getEndOfTermGrade() {
return endOfTermGrade
}
public void setEndOfTermGrade(int endOfTermGrade) {
this.endOfTermGrade = endOfTermGrade
}
public int getTotalGrade() {
return totalGrade
}
@Override
public String toString() {
return "JavaCourse{" +
"sName='" + sName + '\'' +
", sId='" + sId + '\'' +
", testGrade=" + testGrade +
", regularGrade=" + regularGrade +
", endOfTermGrade=" + endOfTermGrade +
'}'
}
public void showTotalGrad() {
//double四舍五入(0.5->1)转int类型
totalGrade = Integer.parseInt(new BigDecimal((testGrade * TR + regularGrade * RR + endOfTermGrade * ER)).setScale(0, BigDecimal.ROUND_HALF_UP).toString())
System.out.println("课差庆程的总成绩:" + totalGrade)
}
}
public class DemoTest02 {
public static void main(String[] args) {
JavaCourse student1 = new JavaCourse()
student1.setsName("小张虚雹握")
student1.setsId("10086")
student1.setTestGrade(85)
student1.setRegularGrade(90)
student1.setEndOfTermGrade(86)
System.out.println(student1.toString())
student1.showTotalGrad()
JavaCourse student2 = new JavaCourse("老王","10010",60,80,45)
System.out.println(student2.toString())
student2.showTotalGrad()
}
}
1,保证主线程阻塞,即并州在执行main方法里添加阻塞代码。
2,比如Java做服务器时,接收soket信息,
ServerSocket serverSocket =newServerSocket(10086)//1024-65535的某个端口
//2、调用accept()方法开始监听,等待客户端的连接 明野
while(true){}
Socket socket = serverSocket.accept()
//3、获取输入流,激蔽喊并读取客户端信息
InputStream is = socket.getInputStream()
InputStreamReader isr =newInputStreamReader(is)
BufferedReader br =newBufferedReader(isr)
String info =null
while((info=br.readLine())!=null){
System.out.println("我是服务器,客户端说:"+info);
}
}
3,添加阻塞代码:
Semaphore semaphore = new Semaphore(0)try {
semaphore.acquire()
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace()
}
这段代码是不占cpu的。它会让出cpu的执行权。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)