javatimere的简单介绍
本篇文章给大家谈谈javatimere,以及对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、如何关闭一个正在accept的ServerSocket
- 2、java中Time和TimeTask的使用
- 3、java中10分钟一个时间段,怎么判断两个时间在同一时间段内
- 4、用java做一个日历显示数据功能,在页面上显示日期数据。
- 5、java判断两个时间字符串是否为同一天
- 6、急求,用JAVA编写,键盘输入生日,用LocalDate写
如何关闭一个正在accept的ServerSocket
加入一个ServerSocket正在另一个线程堵塞accept,那如何停止accept或者关闭Socket?
Server socket 设置下超时
setSoTimeout
然后在Listen线程中用interrupt
其实直接close socket也可以,不过会抛出异常,我的意思是有什么比较安全而又简单的办法?难道要加一个标志,然后要关闭的时候把标志设为stop,然后连接listernSocket?这样也未免太麻烦了
JDK5.0里面新增了java.util.concurrent包(对于多线程的开发建议尽量使用这个包),下面是javadoc里面的样例代码
用法示例
下面给出了一个网络服务的简单结构,这里线程池中的线程作为传入的请求。它使用了预先配置的 Executors.newFixedThreadPool(int) 工厂方法:
java 代码
class NetworkService implements Runnable {
private final ServerSocket serverSocket;
private final ExecutorService pool;
public NetworkService(int port, int poolSize)
throws IOException {
serverSocket = new ServerSocket(port);
pool = Executors.newFixedThreadPool(poolSize);
}
public void run() { // run the service
try {
for (;;) {
pool.execute(new Handler(serverSocket.accept()));
}
} catch (IOException ex) {
pool.shutdown();
}
}
}
class Handler implements Runnable {
private final Socket socket;
Handler(Socket socket) { this.socket = socket; }
public void run() {
// read and service request on socket
}
}
下列方法分两个阶段关闭 ExecutorService。第一阶段调用 shutdown 拒绝传入任务,然后调用 shutdownNow(如有必要)取消所有遗留的任务:
java 代码
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
内存一致性效果:线程中向 ExecutorService 提交 Runnable 或 Callable 任务之前的操作 happen-before 由该任务所提取的所有操作,后者依次 happen-before 通过 Future.get() 获取的结果。
java中Time和TimeTask的使用
static int i = 0;
Timer timer = new Timer();
timer.schedule(new MyTask(),1000,30*60*1000);//定义一个定时器,一秒后运行,每隔30分钟运行一次。
public class MyTask extend TimerTask(){
......................................//这是执行的代码
i = i+1;
if(i=10){
timer.cancel();//关闭定时器
}
}
java中10分钟一个时间段,怎么判断两个时间在同一时间段内
你知道怎么获取格林威治时间吗? 你这两个时间都获取格林威治时间 之后两个时间做差, 得到的数字在转换成时间在得到分钟数 如果大于10就不在一个时间. 如果小于10就在一个时间段
用java做一个日历显示数据功能,在页面上显示日期数据。
MainFrame.java是显示日历程序,Clock.java是日历计算程序(可以不要)。
编译后运行MainFrame这个类即可。
swing窗口显示万年历,jdk1.4以上环境编译运行。
package org.test;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.util.Calendar;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class MainFrame extends JFrame {
private static final long serialVersionUID = -306484324260972141L;
JPanel panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
JPanel panel2 = new JPanel(new GridLayout(7, 7));
JPanel panel3 = new JPanel();
JLabel[] label = new JLabel[49];
JLabel y_label = new JLabel("年份");
JLabel m_label = new JLabel("月份");
JComboBox com1 = new JComboBox();
JComboBox com2 = new JComboBox();
int re_year, re_month, x_size, y_size;
String year_num;
Calendar now = Calendar.getInstance(); // 实例化Calendar
MainFrame() {
super("万年历");
setSize(300, 350);
x_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth());
y_size = (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight());
setLocation((x_size - 300) / 2, (y_size - 350) / 2);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel1.add(y_label);
panel1.add(com1);
panel1.add(m_label);
panel1.add(com2);
for (int i = 0; i 49; i++) {
label[i] = new JLabel("", JLabel.CENTER);// 将显示的字符设置为居中
panel2.add(label[i]);
}
panel3.add(new Clock(this));
panel.add(panel1, BorderLayout.NORTH);
panel.add(panel2, BorderLayout.CENTER);
panel.add(panel3, BorderLayout.SOUTH);
panel.setBackground(Color.white);
panel1.setBackground(Color.white);
panel2.setBackground(Color.white);
panel3.setBackground(Color.white);
Init();
com1.addActionListener(new ClockAction());
com2.addActionListener(new ClockAction());
setContentPane(panel);
setVisible(true);
setResizable(false);
}
class ClockAction implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
int c_year, c_month, c_week;
c_year = Integer.parseInt(com1.getSelectedItem().toString()); // 得到当前所选年份
c_month = Integer.parseInt(com2.getSelectedItem().toString()) - 1; // 得到当前月份,并减1,计算机中的月为0-11
c_week = use(c_year, c_month); // 调用函数use,得到星期几
Resetday(c_week, c_year, c_month); // 调用函数Resetday
}
}
public void Init() {
int year, month_num, first_day_num;
String log[] = { "日", "一", "二", "三", "四", "五", "六" };
for (int i = 0; i 7; i++) {
label[i].setText(log[i]);
}
for (int i = 0; i 49; i = i + 7) {
label[i].setForeground(Color.red); // 将星期日的日期设置为红色
}
for (int i = 6; i 49; i = i + 7) {
label[i].setForeground(Color.green);// 将星期六的日期设置为绿色
}
for (int i = 1; i 10000; i++) {
com1.addItem("" + i);
}
for (int i = 1; i 13; i++) {
com2.addItem("" + i);
}
month_num = (int) (now.get(Calendar.MONTH)); // 得到当前时间的月份
year = (int) (now.get(Calendar.YEAR)); // 得到当前时间的年份
com1.setSelectedIndex(year - 1); // 设置下拉列表显示为当前年
com2.setSelectedIndex(month_num); // 设置下拉列表显示为当前月
first_day_num = use(year, month_num);
Resetday(first_day_num, year, month_num);
}
public int use(int reyear, int remonth) {
int week_num;
now.set(reyear, remonth, 1); // 设置时间为所要查询的年月的第一天
week_num = (int) (now.get(Calendar.DAY_OF_WEEK));// 得到第一天的星期
return week_num;
}
@SuppressWarnings("deprecation")
public void Resetday(int week_log, int year_log, int month_log) {
int month_day_score; // 存储月份的天数
int count;
month_day_score = 0;
count = 1;
Date date = new Date(year_log, month_log + 1, 1); // now
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.MONTH, -1); // 前个月
month_day_score = cal.getActualMaximum(Calendar.DAY_OF_MONTH);// 最后一天
for (int i = 7; i 49; i++) { // 初始化标签
label[i].setText("");
}
week_log = week_log + 6; // 将星期数加6,使显示正确
month_day_score = month_day_score + week_log;
for (int i = week_log; i month_day_score; i++, count++) {
label[i].setText(count + "");
}
}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
new MainFrame();
}
}
package org.test;
import java.awt.Color;
import java.util.Calendar;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.Graphics;
import java.text.SimpleDateFormat;
public class Clock extends Canvas implements Runnable{
private static final long serialVersionUID = 3660124045489727166L;
MainFrame mf;
Thread t;
String time;
public Clock(MainFrame mf){
this.mf=mf;
setSize(280,40);
setBackground(Color.white);
t=new Thread(this);//实例化线程
t.start();
//调用线程
}
public void run(){
while(true){
try{
Thread.sleep(1000);//休眠1秒钟
}catch(InterruptedException e){
System.out.println("异常");
}
this.repaint(100);
}
}
public void paint(Graphics g){
Font f=new Font("宋体",Font.BOLD,16);
SimpleDateFormat SDF=new SimpleDateFormat("yyyy'年'MM'月'dd'日'HH:mm:ss");//格式化时间显示类型
Calendar now=Calendar.getInstance();
time=SDF.format(now.getTime()); //得到当前日期和时间
g.setFont(f);
g.setColor(Color.orange);
g.drawString(time,45,25);
}
}
java判断两个时间字符串是否为同一天
直接用 Calendar 类去比较,是最全的。
java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date date1 = df.format("2013-01-01 01:01:01");
java.util.Date date2 = df.format("2013-02-02 02:02:02");
java.util.Calendar c1 = java.util.Calendar.getInstance();
c1.setTime(date1.getTime());
java.util.Calendar c2 = java.util.Calendar.getInstance();
c2.setTime(date2.getTime());
//这里面的 after 在此条件下得出的结果为 false,也即 date2 date1
boolean after = c1.after(c2);
急求,用JAVA编写,键盘输入生日,用LocalDate写
不太懂你什么意思,LocalDate 是类型,只能说最后用LocalDate 来接收,你可以接收一个生日字符串,然后通过LocalDate.parse("2022-09-23",DateTimeFormatter.ofPattern("yyyy-MM-dd")) 来转换成LocalDate,或者直接接收3个int类型变量,通过LocalDate.of(2022,9,23)来创建一个LocalDate日期。
关于javatimere和的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。