??xml version="1.0" encoding="utf-8" standalone="yes"?> In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).
Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.
In fixed-delay execution, each execution is scheduled relative to the actual execution time of the previous execution. If an execution is delayed for any reason (such as garbage collection or other background activity), subsequent executions will be delayed as well. In the long run, the frequency of execution will generally be slightly lower than the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).
Fixed-delay execution is appropriate for recurring activities that require "smoothness." In other words, it is appropriate for activities where it is more important to keep the frequency accurate in the short run than in the long run. This includes most animation tasks, such as blinking a cursor at regular intervals. It also includes tasks wherein regular activity is performed in response to human input, such as automatically repeating a character as long as a key is held down.
In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).
Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.
In fixed-rate execution, each execution is scheduled relative to the scheduled execution time of the initial execution. If an execution is delayed for any reason (such as garbage collection or other background activity), two or more executions will occur in rapid succession to "catch up." In the long run, the frequency of execution will be exactly the reciprocal of the specified period (assuming the system clock underlying Object.wait(long) is accurate).
Fixed-rate execution is appropriate for recurring activities that are sensitive to absolute time, such as ringing a chime every hour on the hour, or running scheduled maintenance every day at a particular time. It is also appropriate for recurring activities where the total time to perform a fixed number of executions is important, such as a countdown timer that ticks once every second for ten seconds. Finally, fixed-rate execution is appropriate for scheduling multiple repeating timer tasks that must remain synchronized with respect to one another.
Rectangle rect = comp.getBounds();
BufferedImage bufImage = new BufferedImage(rect.width,
rect.height,
BufferedImage.TYPE_INT_RGB);
Graphics g = bufImage.getGraphics();
g.translate(-rect.x, -rect.y);
comp.paint(g);
q样QJComponent中的囑փ׃存到BufferedImage中了?br />原文的链接:http://dev.csdn.net/article/13/13531.shtm
]]>
]]>
]]>
<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="java.awt.*, java.text.*, java.util.*" %>
<%@ page import="org.jfree.chart.*" %>
<%@ page import="org.jfree.chart.axis.*" %>
<%@ page import="org.jfree.chart.labels.StandardCategoryItemLabelGenerator" %>
<%@ page import="org.jfree.chart.plot.*" %>
<%@ page import="org.jfree.chart.renderer.*" %>
<%@ page import="org.jfree.chart.servlet.ServletUtilities" %>
<%@ page import="org.jfree.data.DefaultCategoryDataset" %>
<%@ page import="org.jfree.ui.TextAnchor" %>
<%
//The data for the bar chart
double[] data =
{85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels =
{"Mon", "Tue", "Wed", "Thu", "Fri"};
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < data.length; i++)
{
dataset.addValue(data[i], null, labels[i]);
}
JFreeChart chart = ChartFactory.createBarChart3D("Weekly Server Load", "Work Week 25", "MBytes", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setBackgroundPaint(new Color(0xE1E1E1));
CategoryPlot plot = chart.getCategoryPlot();
// 讄Y轴显C整?/SPAN>
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryAxis domainAxis = plot.getDomainAxis();
//讄距离囄左端距离
domainAxis.setLowerMargin(0.05);
BarRenderer3D renderer = new BarRenderer3D();
//讄q颜色
renderer.setSeriesPaint(0, new Color(0xff00));
plot.setRenderer(renderer);
String filename = ServletUtilities.saveChartAsPNG(chart, 300, 280, null, session);
String graphURL = request.getContextPath() + "/displayChart?filename=" + filename;
%>
<html>
<body topmargin="5" leftmargin="5" rightmargin="0">
<div style="font-size:18pt; font-family:verdana; font-weight:bold">
3D Bar Chart
</div>
<br>
<img src="<%= graphURL %>" border=0>
</body>
</html>
d来的图:
和ChartDirectord来的囑ց一个比较:
]]>
范例E序Q?BR>
<%@page import="ChartDirector.*" %>
<%
//The data for the bar chart
double[] data =
{85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels =
{"Mon", "Tue", "Wed", "Thu", "Fri"};
//Create a XYChart object of size 300 x 280 pixels
XYChart c = new XYChart(300, 280);
//Set the plotarea at (45, 30) and of size 200 x 200 pixels
c.setPlotArea(45, 30, 200, 200);
//Add a title to the chart
c.addTitle("Weekly Server Load");
//Add a title to the y axis
c.yAxis().setTitle("MBytes");
//Add a title to the x axis
c.xAxis().setTitle("Work Week 25");
//Add a bar chart layer with green (0x00ff00) bars using the given data
c.addBarLayer(data, 0xff00).set3D();
//Set the labels on the x axis.
c.xAxis().setLabels(labels);
//output the chart
String chart1URL = c.makeSession(request, "chart1");
//include tool tip for the chart
String imageMap1 = c.getHTMLImageMap("", "", "title='{xLabel}: {value} MBytes'")
;
%>
<html>
<body topmargin="5" leftmargin="5" rightmargin="0">
<div style="font-size:18pt; font-family:verdana; font-weight:bold">
3D Bar Chart
</div>
<hr color="#000080">
<a href="viewsource.jsp?file=<%=request.getServletPath()%>">
<font size="2" face="Verdana">View Chart Source Code</font>
</a>
</div>
<br>
<img src='<%=response.encodeURL("getchart.jsp?"+chart1URL)%>'
usemap="#map1" border="0">
<map name="map1"><%=imageMap1%></map>
</body>
</html>
如果要在q剙昄数|可以调用Layer的setDataLabelFormatҎ讄Q范例:layer.setDataLabelFormat("{value}");
其它的例子可以参考它的文档的说明。ChartDirector的网址Q?A >http://www.advsofteng.com
]]>
class Worker extends TimerTask
{
public void run()
{
System.out.println("我在工作啦!");
}
}
TimercȝscheduleҎ或者scheduleAtFixedRateҎ启动定时执行Qschedule重蝲了四个版本,scheduleAtFixedRate重蝲了两个。每个方法的实现都不同,下面是每个方法的说明Q?BR>
schedule
public void schedule(TimerTask task,
long delay)
说明Q该Ҏ会在讑֮的g时后执行一ơQ务?BR>
task
- task to be scheduled.
delay
- delay in milliseconds before task is to be executed.
IllegalArgumentException
- if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException
- if task was already scheduled or cancelled, or timer was cancelled.
schedule
public void schedule(TimerTask task,
Date time)
说明Q该Ҏ会在指定的时间点执行一ơQ务?BR>
task
- task to be scheduled.
time
- time at which task is to be executed.
IllegalArgumentException
- if time.getTime() is negative.
IllegalStateException
- if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
schedule
public void schedule(TimerTask task,
long delay,
long period)
说明Q该Ҏ会在指定的g时后执行dQƈ且在讑֮的周期定时执行Q务?BR>
task
- task to be scheduled.
delay
- delay in milliseconds before task is to be executed.
period
- time in milliseconds between successive task executions.
IllegalArgumentException
- if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException
- if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
schedule
public void schedule(TimerTask task,
Date firstTime,
long period)
说明Q该Ҏ会在指定的时间点执行dQ然后从该时间点开始,在设定的周期定时执行d。特别的Q如果设定的旉点在当前旉之前QQ务会被马上执行,然后开始按照设定的周期定时执行d?BR>
task
- task to be scheduled.
firstTime
- First time at which task is to be executed.
period
- time in milliseconds between successive task executions.
IllegalArgumentException
- if time.getTime() is negative.
IllegalStateException
- if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
scheduleAtFixedRate
public void scheduleAtFixedRate(TimerTask task,
long delay,
long period)
说明Q该Ҏ和schedule的相同参数的版本cMQ不同的是,如果该Q务因为某些原因(例如垃圾攉Q而gq执行,那么接下来的d会尽可能的快速执行,以赶上特定的旉炏V?BR>
task
- task to be scheduled.
delay
- delay in milliseconds before task is to be executed.
period
- time in milliseconds between successive task executions.
IllegalArgumentException
- if delay is negative, or delay + System.currentTimeMillis() is negative.
IllegalStateException
- if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
scheduleAtFixedRate
public void scheduleAtFixedRate(TimerTask task,
Date firstTime,
long period)
说明Q和上一个方法类伹{?BR>
task
- task to be scheduled.
firstTime
- First time at which task is to be executed.
period
- time in milliseconds between successive task executions.
IllegalArgumentException
- if time.getTime() is negative.
IllegalStateException
- if task was already scheduled or cancelled, timer was cancelled, or timer thread terminated.
下面是我的一个测试片断:
public static void main(String[] args) throws Exception
{
Timer timer = new Timer(false);
timer.schedule(new Worker(), new Date(System.currentTimeMillis() + 1000));
}
]]>
]]>
Object obj1 = "abc";
Object obj2 = "cde";
if ((obj1 == null && obj2 == null) || (obj1 != null && obj1.equals(obj2))
|| (obj2 != null && obj2.equals(obj1)))
{
System.out.println("equals");
}
q样的程序,读v来真是挺拗口。我一直没有想C么好的方法解册个问题,直到今天在看到JDK的AbstractList源码的equalsҎ的实现时Q看到这一D:
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
原来用三元运符可以很好的解册个问题,呵呵Q我前面的程序可以改写成Q?BR>
Object obj1 = "abc";
Object obj2 = "cde";
if (obj1 == null ? obj2 == null : obj1.equals(obj2))
System.out.println("equals");
真是z多了!
]]>
2.在用非dIOӞ需要把该channel讄成非d的,即需要调用SocketChannel.configureBlocking(false);
3.如果该channel注册了selectorQ那么在q回该实例到对象池中Q需要把注册的selector清除Q即需要调用Selector的closeҎ?BR>
下面是一D应用场景的例子Q?BR>
// 把命令输?/SPAN>
channel.configureBlocking(true);
PrintWriter writer = new PrintWriter(channel.socket().getOutputStream(), false);
writer.write(command.endsWith("\n") ? command : command + "\n");
writer.flush();
channel.configureBlocking(false);
// 创徏Selector
Selector selector = Selector.open();
// 向Selector注册我们需要的READ事g
SelectionKey skey = channel.register(selector, SelectionKey.OP_READ);
boolean stop = false;
int n = 0;
int read = 0;
ByteBuffer buffer = ByteBuffer.allocate(1024);
// 轮询
while (!stop)
{
// 获取Selectorq回的时间?/SPAN>
n = selector.select();
// 当传回的值大?事,M件发生了
if (n > 0)
{
// 处理发生的事?/SPAN>
}
}
selector.close();
]]>
package sample.proxy;
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public interface TestInterface
{
public String print();
}
package sample.proxy;
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public class TestImpl implements TestInterface
{
public String print()
{
return "Hello, it's from TestImpl class";
}
}
下面拦截printҎQ调用自q实现Q这需要实现java.lang.reflect.InvocationHandler接口?BR>
package sample.proxy;
import java.lang.reflect.*;
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public class TestHandler implements InvocationHandler
{
TestInterface test;
/**//**
* 动态代理绑定到指定的TestInterface
* @param test TestInterface
* @return TestInterface l定代理后的TestInterface
*/
public TestInterface bind(TestInterface test)
{
this.test = test;
TestInterface proxyTest = (TestInterface) Proxy.newProxyInstance(
test.getClass().getClassLoader(), test.getClass().getInterfaces(), this);
return proxyTest;
}
/**//**
* Ҏ调用拦截器,拦截printҎ
* @param proxy Object
* @param method Method
* @param args Object[]
* @return Object
* @throws Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
// 如果调用的是printҎQ则替换?/SPAN>
if ("print".equals(method.getName()))
{
return "HaHa, It's come from TestHandler";
} else
{
return method.invoke(this.test, args);
}
}
}
下面是测试用例:
package sample.test;
import junit.framework.*;
import sample.proxy.*;
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public class TestDynamicProxy extends TestCase
{
private TestInterface test = null;
protected void setUp() throws Exception
{
super.setUp();
TestHandler handler = new TestHandler();
// 用handlerȝ成实?/SPAN>
test = handler.bind(new TestImpl());
}
protected void tearDown() throws Exception
{
test = null;
super.tearDown();
}
public void testPrint()
{
System.out.println(test.print());
}
}
q行试用例Q可以看到输出的是“HaHa, It's come from TestHandler”?img src ="http://www.aygfsteel.com/georgehill/aggbug/5126.html" width = "1" height = "1" />
]]>
]]>
package sample.nio;
import java.io.*;
import java.net.*;
import java.nio.channels.*;
import java.util.*;
/**//**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author George Hill
* @version 1.0
*/
public class Server
{
private int port;
public Server(int port)
{
this.port = port;
}
public void startServer() throws IOException
{
// 创徏ServerSocketChannelq且l定到指定的端口
ServerSocketChannel ssc = ServerSocketChannel.open();
InetSocketAddress address = new InetSocketAddress(InetAddress.getLocalHost(), port);
ssc.socket().bind(address);
ssc.configureBlocking(false);
// 创徏SelectorQƈ且注册ACCEPT事g
Selector selector = Selector.open();
SelectionKey skey = ssc.register(selector, SelectionKey.OP_ACCEPT);
boolean stop = false;
int n = 0;
System.out.println("Server Start");
// 轮询
while (!stop)
{
// 获取Selectorq回的时间?/SPAN>
n = selector.select();
// 当传回的值大?事,L间发生了
if (n > 0)
{
Set set = selector.selectedKeys();
Iterator it = set.iterator();
while (it.hasNext())
{
skey = (SelectionKey) it.next();
it.remove();
if (skey.isAcceptable())
{
// 从channel()中取得刚刚注册的Channel
Socket socket = ((ServerSocketChannel) skey.channel()).accept().socket();
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
// ?Hello, World"写入
writer.write("Hello, World!\n");
// 睡眠3U?/SPAN>
try
{
Thread.sleep(3000);
} catch (InterruptedException ie)
{
}
// ?EXIT"写入Buffer
writer.write("EXIT");
// 退出程?/SPAN>
writer.close();
// stop = true;
}
}
}
}
ssc.close();
System.out.println("Server Stop");
}
public static void main(String[] args) throws Exception
{
Server server = new Server(5000);
server.startServer();
}
}
]]>
]]>
?.0中的server.xml文g格式如下Q?/P>