Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The System
class provides several methods that provide miscellaneous
functionality including copying arrays and getting the current time.
UseSystem
'sarraycopy
method to efficiently copy data from one array into another. For more information, refer to Copying Arrays.
ThecurrentTimeMillis
method returns the current time in milliseconds since 00:00:00, January 1, 1970. ThecurrentTimeMillis
method is commonly used during performance tests: get the current time, perform the operation that you want to time, get the current time again--the difference in the two time samples is roughly the amount of time that the operation took to perform.Often in graphical user interfaces the time difference between mouse clicks is used to determine whether a user double clicked. The following applet uses
currentTimeMillis
to compute the number of milliseconds between two mouse clicks. If the time period between the clicks is smaller than 200 milliseconds, the two mouse clicks are interpreted as a double mouse click.
Here's
Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or JDK. We strongly recommend that you install the latest version; at least 1.4.2 is required for all our applets. You can find more information in the Java Plug-in home page.the source
for theTimingIsEverything
applet:import java.awt.Graphics; import java.awt.Dimension; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; public class TimingIsEverything extends java.applet.Applet { public long firstClickTime = 0; public String displayStr; public void init() { displayStr = "Double Click Me"; addMouseListener(new MyAdapter()); } public void paint(Graphics g) { g.drawRect(0, 0, getSize().width-1, getSize().height-1); g.drawString(displayStr, 40, 30); } class MyAdapter extends MouseAdapter { public void mouseClicked(MouseEvent evt) { long clickTime = System.currentTimeMillis(); long clickInterval = clickTime - firstClickTime; if (clickInterval < 300) { displayStr = "Double Click!! (Interval = " + clickInterval + ")"; firstClickTime = 0; } else { displayStr = "Single Click!!"; firstClickTime = clickTime; } repaint(); } } }You could use the return value from this method to compute the current date and time. However, you'll probably find that it's more convenient to get the current date and time from the
Date
class.
Note: Previous versions of theSystem
class support two other time-related methods besides thecurrentTimeMillis
method:currentTime
andnowMillis
. These two methods are now obsolete--you should usecurrentTimeMillis
instead.
To exit the Java interpreter, call theSystem.exit
method. The interpreter exits with the integer exit code that you specify to theexit
method.Note: TheSystem.exit(-1);exit
method causes the Java interpreter to exit, not just your Java program. Use this function with caution.
Security consideration: Invocation of theexit
method is subject to security restrictions. Depending on the browser that an applet is running in, a call toexit
from within an applet will likely result in aSecurityException
. See Security Restrictions for information about the security restrictions placed on applets.
The security manager is an object that enforces a certain security policy for a Java application. You can set the current security manager for your applications usingSystem
'ssetSecurityManager
method, and you can retrieve the current security manager usingSystem
'sgetSecurityManager
method. An application's security manager is ajava.lang.SecurityManager
object.Providing Your Own Security Manager discusses security managers in detail and shows you how to create and install your own security manager using these two
System
methods.
Security consideration: The security manager for an application can be set only once. Typically, a browser sets its security manager during its startup procedure. Thus, most of the time, applets cannot set the security manager because it's already been set. ASecurityException
will result if your applet attempts to do so. See Getting Started for information about the security restrictions placed on applets.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.