鎖加單例:
平度網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),平度網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為平度近1000家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的平度做網(wǎng)站的公司定做!
public?class?Regist{
private?static?Regist?rgst=new?Regist();
private?Regist(){}
private?int?cnt=0;
private?int?max=100;
public?static?getRegist(){return?rgst;}
///設(shè)置最大上線人數(shù)
public?void?setMax(int?max){this.max=max;}
///判斷新用戶能否登入
public?synchronized?boolean?login(){
if(cnt=max)return?false;
++cnt;
return?true;
}
///用戶退出時(shí)調(diào)用
public?synchronized?void?logout(){
--cnt;
}
};
有新用戶要登入時(shí),先Regist::getRegist().login()判斷能否登入,返回true則表示沒到用戶上限,可以登入,否則表示到上限了,不能登入,用戶退出時(shí)需要調(diào)用Regist::getRegist().logout()
synchronized不清楚可以百度,是種類似鎖的東西。作用于方法上,相當(dāng)于調(diào)用方法前先lock,調(diào)用之后unlock,其鎖的對(duì)象是該方法的類對(duì)象。
線程不是太多可以用這個(gè)方法,線程太多用這個(gè)可能慢了些,好像加鎖比較耗時(shí)間,如果同一個(gè)時(shí)間太多人進(jìn)行登入操作,都得在那等著,一個(gè)一個(gè)進(jìn)行,反應(yīng)慢
public static void main(String[] args)
{
int a[]=new int[10];
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入十個(gè)數(shù)字0~100");
for(int i=0;i10;i++)
{
System.out.println("輸入第"+(i+1)+"數(shù):");
a[i]=sc.nextInt();
}
int b[]=new int[11];
for(int i=0;i11;i++)
{
b[i]=0;
}
for(int i=0;i10;i++)
{
if(a[i]=0a[i]=9)
{
b[0]++;
}
else if(a[i]=10a[i]=19)
{
b[1]++;
}
else if(a[i]=20a[i]=29)
{
b[2]++;
}
else if(a[i]=30a[i]=39)
{
b[3]++;
}
else if(a[i]=40a[i]=49)
{
b[4]++;
}
else if(a[i]=50a[i]=59)
{
b[5]++;
}
else if(a[i]=60a[i]=69)
{
b[6]++;
}
else if(a[i]=70a[i]=79)
{
b[7]++;
}
else if(a[i]=80a[i]=89)
{
b[8]++;
}
else if(a[i]=90a[i]=99)
{
b[9]++;
}
else if(a[i]==100)
{
b[10]++;
}
}
System.out.println("0~9人數(shù):"+b[0]+";10~19人數(shù):"+b[1]+";20~29人數(shù):"+b[2]+";30~39人數(shù):"+b[3]+";40~49人數(shù):"+b[4]+";50~59人數(shù):"+b[5]+";60~69人數(shù):"+b[6]+";70~79人數(shù):"+b[7]+";80~89人數(shù):"+b[8]+";90~99人數(shù):"+b[9]+";100人數(shù):"+b[10]);
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人數(shù)為:"+b[i]);
}
}
簡(jiǎn)單的方法是:
public static void main(String[] args)
{
int a[]=new int[11];
Scanner sc = new Scanner(System.in);
System.out.println("請(qǐng)輸入十個(gè)數(shù)字0~100");
for(int i=0;i10;i++)
{
System.out.println("輸入第"+(i+1)+"數(shù):");
a[sc.nextInt()/10]++;//對(duì)于這種根據(jù)讀取數(shù)據(jù)分段的數(shù)據(jù)利用int型數(shù)據(jù)整除可以很好的分出每個(gè)段的個(gè)數(shù);
}
for(int i=0;i11;i++)
{
System.out.println("第"+(i+1)+"段人數(shù)為:"+a[i]);
}
}
在類中聲明一個(gè)靜態(tài)變量如下 :
static int num=0 ,每次類初始化將num 加一。
靜態(tài)變量為所有類共享,num的大小即為創(chuàng)建的student對(duì)象的數(shù)目
程序稍微改動(dòng)如下:
class student {
String name;
static int num =0;
String id;
float ywscore;
float sxscore;
float sumscore;
float avgscore;
student(String name,String id,float ywscore,float sxscore){
this.name=name;
this.id=id;
this.ywscore=ywscore;
this.sxscore=sxscore;
num ++;
}
float sum(){
sumscore=sxscore+ywscore;
return sumscore;
}
float avg(){
avgscore=(sxscore+ywscore)/2;
return avgscore;
}
int getNum()
{
return num;
}
void showinfo(){
System.out.println("name: "+name);
System.out.println("id: "+id);
System.out.println("mathscore: "+sxscore);
System.out.println("langue: "+ywscore);
System.out.println("avg: "+avg());
System.out.println("sum: "+sum());
}
};
首先給你講一下,我的理解,人數(shù)統(tǒng)計(jì)好說,因?yàn)椋梢言O(shè)置監(jiān)聽器,讓其在服務(wù)器啟動(dòng)的時(shí)候進(jìn)行監(jiān)聽,然后統(tǒng)計(jì)登錄人數(shù),但是統(tǒng)計(jì)在線人數(shù),就麻煩點(diǎn)了,因?yàn)榇蠖鄶?shù)人都不愿意點(diǎn)擊注銷按鈕,直接關(guān)閉瀏覽器,這樣session的關(guān)閉就麻煩了點(diǎn),我先給出統(tǒng)計(jì)人數(shù)的代碼,我想統(tǒng)計(jì)在線人數(shù)可以定時(shí)調(diào)用程序來統(tǒng)計(jì),但是不是實(shí)時(shí)的,所以最大的障礙就是怎么在關(guān)閉瀏覽器的時(shí)候,就關(guān)閉session
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ApplicationListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
ServletContext sc = arg0.getServletContext();
Integer count = (Integer) sc.getAttribute("AccessCount");
try {
PrintWriter out = new PrintWriter(new FileWriter("c:/Users/god/count.txt"));// 將人數(shù)保存到文本文件
System.out.println("count:"+count);
out.print(count);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void contextInitialized(ServletContextEvent arg0) {
Integer count = 0;
ServletContext sc = arg0.getServletContext();
try {
Scanner in = new Scanner(new FileReader("c:/Users/god/count.txt"));// 讀取已經(jīng)產(chǎn)生的人數(shù)信息,然后進(jìn)行累加
count = in.nextInt();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
sc.setAttribute("AccessCount", count);
}
}
在web.xml文件里面進(jìn)行設(shè)置
listener
listener-class
文件的位置也就是包名.ApplicationListener
/listener-class
/listener
統(tǒng)計(jì)的時(shí)候是設(shè)置攔截器,需要在struts.xml中設(shè)置
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
@SuppressWarnings("serial")
public class AccessInterceptor implements Interceptor{
private String id;
public void destroy() {
}
public void init() {
}
@SuppressWarnings("unchecked")
public String intercept(ActionInvocation ai) throws Exception {
ServletContext sc = ServletActionContext.getServletContext();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
if(!session.getId().equals(id)){
Integer count = (Integer) sc.getAttribute("AccessCount");// 判斷如果登錄成功,統(tǒng)計(jì)數(shù)字自增
sc.setAttribute("AccessCount", count+1);
id = session.getId();
}
String result = ai.invoke();
return result;
}
}