「java每秒操作次数」每分钟有效操作次数
本篇文章给大家谈谈java每秒操作次数,以及每分钟有效操作次数对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java定时器 限制方法调用次数
- 2、Java如何控制方法的调用次数?
- 3、在java中怎样统计一个线程执行的次数和不能执行的次数
- 4、在Java中怎样得出一个按钮点击的次数
- 5、java统计打开次数问题求教
- 6、java中统计一个方法调用多少次
java定时器 限制方法调用次数
添加一个静态常量,每当方法被调用,该常量减1,当该常量为0时,调用该方法就直接退出。
Java如何控制方法的调用次数?
Java控制方法的调用次数,可以使用static变量来统计次数,当达到规定的次数,抛出异常,实例如下:
static int i=0;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//循环调用count计数
for (int i = 0; i 5; i++) {
count();
if(static 10){//当方法调用次数达到这个次数,就终止主程序
return;
}
}
}
/**
* 计数
*/
public static void count(){
i++;
System.out.println("第 "+i+" 次调用count方法");
}
在java中怎样统计一个线程执行的次数和不能执行的次数
思路:
创建线程继承线程类或者实现线程接口
重写run方法
在run方法里面写for循环,
循环语句块中打印线程类的静态方法.currentthread().getname()
+循环的自增值。
,主线程创建自定义对象实例。
调用start()方法ok、。
在Java中怎样得出一个按钮点击的次数
java中得出一个按钮点击的次数,可以使用临时变量来保存点击的次数,在监听事件中对变量进行+1操作,实例如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
public class Test {
int count= 0;
public static void main(String [] args){
JButton A = new JButton("A");
JButton B = new JButton("B");
JButton C = new JButton("C");
A.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
methodA();
count++;
}
});
B.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
methodB();
}
});
C.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
methodA();
methodB();
}
});
}
public static void methodA(){
//执行A方法
}
public static void methodB(){
//执行B方法
}
}
java统计打开次数问题求教
具体我也不会,但有一篇文档或许对你有帮助,这两个都是监听windows(操作系统)的事件,你可以去看看JInvoke.jar,相关的api。下面是监听鼠标操作的例子。补充一下,不要把它想象成javaGUI本身的事件,其实他是在监听windows级别的事件,及底层的事件,可以做一些小病毒的。
import static com.jinvoke.win32.WinConstants.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import com.jinvoke.Callback;
import com.jinvoke.JInvoke;
import com.jinvoke.NativeImport;
import com.jinvoke.Util;
import com.jinvoke.win32.User32;
import com.jinvoke.win32.structs.Msg;
public class MouseHook extends JPanel{
static {
JInvoke.initialize();
}
@NativeImport(library = "user32")
public native static int SetWindowsHookEx (int idHook, Callback hookProc, int hModule, int dwThreadId);
@NativeImport(library = "user32")
public native static int UnhookWindowsHookEx (int idHook);
public static final int WH_MOUSE_LL = 14;
static JFrame frame;
static TextArea mouseEventArea = new TextArea();
static JButton setHookBtn;
static JButton removeHookBtn;
public MouseHook() {
super(new BorderLayout());
mouseEventArea.setText("1) Click the \"Set Mouse Hook\" button.\n" +
"2) Start clicking anywhere on the desktop. Mouse clicks will be captured here.\n" +
"3) Stop the mouse hook by clicking the \"Remove Mouse Hook\" button.\n\n");
JScrollPane MouseEventPane = new JScrollPane(mouseEventArea);
add(MouseEventPane, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
setHookBtn = new JButton("Set Mouse Hook");
setHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
setMouseHook();
}} );
removeHookBtn = new JButton("Remove Mouse Hook");
removeHookBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
unsetMouseHook();
}} );
removeHookBtn.setEnabled(false);
buttonPanel.add(setHookBtn);
buttonPanel.add(removeHookBtn);
add(buttonPanel, BorderLayout.SOUTH);
}
private void setMouseHook() {
setHookBtn.setEnabled(false);
removeHookBtn.setEnabled(true);
// This hook is called in the context of the thread that installed it.
// The call is made by sending a message to the thread that installed the hook.
// Therefore, the thread that installed the hook must have a message loop.
//
// We crate a new thread as we don't want the AWT Event thread to be stuck running a message pump
// nor do we want the main thread to be stuck in running a message pump
Thread hookThread = new Thread(new Runnable(){
public void run() {
if (MouseProc.hookHandle == 0) {
int hInstance = User32.GetWindowLong(Util.getWindowHandle(frame), GWL_HINSTANCE);
MouseProc.hookHandle = SetWindowsHookEx(WH_MOUSE_LL,
new Callback(MouseProc.class, "lowLevelMouseProc"),
hInstance,
0);
// Standard message dispatch loop (message pump)
Msg msg = new Msg();
while (User32.GetMessage(msg, 0, 0, 0)) {
User32.TranslateMessage(msg);
User32.DispatchMessage(msg);
}
} else {
mouseEventArea.append("The Hook is already installed.\n");
}
}});
hookThread.start();
}
private void unsetMouseHook() {
setHookBtn.setEnabled(true);
removeHookBtn.setEnabled(false);
UnhookWindowsHookEx(MouseProc.hookHandle);
MouseProc.hookHandle = 0;
}
private static void createAndShowGUI() {
//Create and set up the window.
frame = new JFrame("Mouse Hook");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MouseHook MouseEventsWindow = new MouseHook();
MouseEventsWindow.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
//Add content to the window.
frame.add(MouseEventsWindow, BorderLayout.CENTER);
//Display the window.
frame.pack();
frame.setBounds(300, 200, 750, 600);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
class MouseProc {
static int hookHandle;
@NativeImport(library = "user32")
public native static int CallNextHookEx (int idHook, int nCode, int wParam, int lParam);
static {
JInvoke.initialize();
}
public static int lowLevelMouseProc(int nCode, int wParam, int lParam ) {
if (nCode 0)
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
if (nCode == HC_ACTION) {
MouseHookStruct mInfo = Util.ptrToStruct(lParam, MouseHookStruct.class);
String message = "Mouse pt: (" + mInfo.pt.x + ", " + mInfo.pt.y + ") ";
switch (wParam) {
case WM_LBUTTONDOWN:
message += "Left button down";
break;
case WM_LBUTTONUP:
message += "Left button up";
break;
case WM_MOUSEMOVE:
message += "Mouse moved";
break;
case WM_MOUSEWHEEL:
message += "Mouse wheel rotated";
break;
case WM_RBUTTONDOWN:
message += "Right button down";
break;
case WM_RBUTTONUP:
message += "Right button down";
break;
}
System.out.println(message);
// MouseHook.mouseEventArea.append(message+"\n");
}
return CallNextHookEx(hookHandle, nCode, wParam, lParam);
}
}
=============================================
import com.jinvoke.NativeStruct;
import com.jinvoke.win32.structs.Point;
@NativeStruct
public class MouseHookStruct {//MSLLHOOKSTRUCT
public Point pt = new Point();
public int mouseData;
public int flags;
public int time;
public int dwExtraInfo;
}
java中统计一个方法调用多少次
总体思路,定义一个静态全局变量来统计方法执行次数,每进方法一次,统计次数加1
所有方法执行完成时,输出统计次数就可以了。
示例代码如下:
public class CountTest {
public static int count1 = 0;
public static int count2 = 0;
public static void main(String[] args) {
Random r = new Random();
for (int i=0; i 10; i++) {
int num = r.nextInt();
if (num 0.5) {
method1();
} else {
method2();
}
}
System.out.println(count1 + " " + count2);
}
public static void method1() {
count1++;
}
public static void method2() {
count2++;
}
}
java每秒操作次数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于每分钟有效操作次数、java每秒操作次数的信息别忘了在本站进行查找喔。