「java新建任务」Java新建工程

博主:adminadmin 2023-01-26 08:09:09 41

今天给各位分享java新建任务的知识,其中也会对Java新建工程进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

javaweb任务列队怎么做

1.在同一时刻,可能有很多任务需要执行,

而程序在同一时刻只能执行一定数量的任务,

当需要执行的任务数超过了程序所能承受的任务数时怎么办呢?

这就有了先执行哪些任务,后执行哪些任务的规则。

2.TaskQueue类就定义了这些规则中的一种,它采用的是FIFO(先进先出,英文名是First

In

First

Out)的方式,也就是按照任务到达的先后顺序执行。TaskQueue类的定义。

Java任务队列代码如下:

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

public class TaskQueue

{

private ListTask

queue = new LinkedListTask();

//

添加一项任务

public synchronized void addTask(Task

task) {

if (task

!= null)

{

queue.add(task);

}

}

//

完成任务后将它从任务队列中删除

public synchronized void finishTask(Task

task) {

if (task

!= null)

{

task.setState(Task.State.FINISHED);

queue.remove(task);

}

}

//

取得一项待执行任务

public synchronized Task

getTask() {

IteratorTask

it = queue.iterator();

Task

task;

while (it.hasNext())

{

task

= it.next(); //

寻找一个新建的任务

if (Task.State.NEW.equals(task.getState()))

{ //

把任务状态置为运行中

task.setState(Task.State.RUNNING);

return task;

}

}

return null;

}

}

import java.util.Iterator;

import java.util.LinkedList;

import java.util.List;

public class TaskQueue

{

private ListTask

queue = new LinkedListTask();

//

添加一项任务

public synchronized void addTask(Task

task) {

if (task

!= null)

{

queue.add(task);

}

}

//

完成任务后将它从任务队列中删除

public synchronized void finishTask(Task

task) {

if (task

!= null)

{

task.setState(Task.State.FINISHED);

queue.remove(task);

}

}

//

取得一项待执行任务

public synchronized Task

getTask() {

IteratorTask

it = queue.iterator();

Task

task;

while (it.hasNext())

{

task

= it.next(); //

寻找一个新建的任务

if (Task.State.NEW.equals(task.getState()))

{ //

把任务状态置为运行中

task.setState(Task.State.RUNNING);

return task;

}

}

return null;

}

}

「java新建任务」Java新建工程

JAVA中如何设置等待时间(非线程)

java中使用用线程控制Task任务,启动下面的线程就可以了,new Thread(new Task()).start() ;public class

Task implements Runnable {//新建一个任务

private TextArea textArea;

public Task(TextArea textArea){

this.textArea = textArea;

}

public void run() {

while (true) {

this.textArea.setText("这里设置: 输出的一段文字");

try {

Thread.sleep(500); // 这里设置:隔多长时间

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

JAVA制作一个任务列表

JAVA 的列表 方式 迅雷的 是执行任务后定时排队列表

以下思路 ,

1)java.util.Timer.

2)ServletContextListener.

3)org.springframework.scheduling.timer.ScheduledTimerTask

1)java.util.Timer

这个方法应该是最常用的,不过这个方法需要手工启动你的任务:

Timer timer=new Timer();

timer.schedule(new ListByDayTimerTask(),10000,86400000);

这里的ListByDayTimerTask类必须extends TimerTask里面的run()方法。

2)ServletContextListener

这个方法在web容器环境比较方便,这样,在web server启动后就可以

自动运行该任务,不需要手工操作。

将ListByDayListener implements ServletContextListener接口,在

contextInitialized方法中加入启动Timer的代码,在contextDestroyed

方法中加入cancel该Timer的代码;然后在web.xml中,加入listener:

listener

listener-classcom.sysnet.demo.util.MyTimerTask/listener-class

/listener

3)org.springframework.scheduling.timer.ScheduledTimerTask

如果你用spring,那么你不需要写Timer类了,在schedulingContext-timer

.xml中加入下面的内容就可以了:

?xml version="1.0" encoding="UTF-8"?

!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" ""

beans

bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"

property name="scheduledTimerTasks"

list

ref local="MyTimeTask1"/

/list

/property

/bean

bean id="MyTimeTask" class="com.qq.timer.ListByDayTimerTask"/

bean id="MyTimeTask1" class="org.springframework.scheduling.timer.ScheduledTimerTask"

property name="timerTask"

ref bean="MyTimeTask"/

/property

property name="delay"

value10000/value

/property

property name="period"

value86400000/value

/property

/bean

/beans

下面给出方法2的一个例子供大家参考:

Java代码:

import java.util.Timer;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

public class MyTimerTask implements ServletContextListener{

private Timer timer = null;

@Override

public void contextDestroyed(ServletContextEvent event) {

// TODO Auto-generated method stub

timer.cancel();

event.getServletContext().log("定时器销毁");

System.out.println("停止备份程序……");

}

@Override

public void contextInitialized(ServletContextEvent event) {

//在这里初始化监听器,在tomcat启动的时候监听器启动,考试,大提示可以在这里实现定时器功能

timer = new Timer(true);

event.getServletContext().log("定时器已启动");//添加日志,可在tomcat日志中查看到

timer.schedule(new exportHistoryBean(event.getServletContext()),0,5*1000);//调用exportHistoryBean,0表示任务无延迟,5*1000表示每隔5秒执行任务,60*60*1000表示一个小时;

}

}

import java.util.Calendar;

import java.util.TimerTask;

import javax.servlet.ServletContext;

public class exportHistoryBean extends TimerTask

{

private static final int C_SCHEDULE_HOUR = 0;

private static boolean isRunning = false;

private ServletContext context = null;

public exportHistoryBean(ServletContext context)

{

this.context = context;

}

@Override

public void run()

{

Calendar c = Calendar.getInstance();

if(!isRunning)

{

if(C_SCHEDULE_HOUR == c.get(Calendar.HOUR_OF_DAY))

{

isRunning = true;

context.log("开始执行指定任务");

isRunning = false;

context.log("指定任务执行结束");

}

else

{

context.log("上一次任务执行还未结束");

}

}

}

}

web.xml里加入一下代码:

listener

listener-classcom.sysnet.demo.util.MyTimerTask/listener-class

/listener

java代码启动一个定时任务

在应用里经常都有用到在后台跑定时任务的需求。举个例子,比如需要在服务后台跑一个定时任务来进行非实时计算,清除临时数据、文件等。在本文里,3种不同的实现方法:

普通thread实现

TimerTask实现

ScheduledExecutorService实现

1.普通thread

这是最常见的,创建一个thread,然后让它在while循环里一直运行着,通过sleep方法来达到定时任务的效果。这样可以快速简单的实现,代码如下:

public class Task1 {

public static void main(String[] args) {

// run in a second

final long timeInterval = 1000;

Runnable runnable = new Runnable() {

public void run() {

while (true) {

// ------- code for task to run

System.out.println("Hello !!");

// ------- ends here

try {

Thread.sleep(timeInterval);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

};

Thread thread = new Thread(runnable);

thread.start();

}

}

2.用Timer和TimerTask

上面的实现是非常快速简便的,但它也缺少一些功能。

用Timer和TimerTask的话与上述方法相比有如下好处:

当启动和去取消任务时可以控制

第一次执行任务时可以指定你想要的delay时间

在实现时,Timer类可以调度任务,TimerTask则是通过在run()方法里实现具体任务。

Timer实例可以调度多任务。

当Timer的构造器被调用时,创建了一个线程,这个线程可以用来调度任务:

import java.util.Timer;

import java.util.TimerTask;

public class Task2 {

public static void main(String[] args) {

TimerTask task = new TimerTask() {

@Override

public void run() {

// task to run goes here

System.out.println("Hello !!!");

}

};

Timer timer = new Timer();

long delay = 0;

long intevalPeriod = 1 * 1000;

// schedules the task to be run in an interval

timer.scheduleAtFixedRate(task, delay,

intevalPeriod);

} // end of main

}

3.ScheduledExecutorService

ScheduledExecutorService是从Java SE 5的java.util.concurrent里,做为并发工具类被引进的,这是最理想的定时任务实现方式。

相比于上两个方法,它有以下好处:

相比于Timer的单线程,它是通过线程池的方式来执行任务的

可以很灵活的去设定第一次执行任务delay时间

提供了良好的约定,以便设定执行的时间间隔

通过ScheduledExecutorService#scheduleAtFixedRate展示这个例子,通过代码里参数的控制,首次执行加了delay时间:

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

public class Task3 {

public static void main(String[] args) {

Runnable runnable = new Runnable() {

public void run() {

// task to run goes here

System.out.println("Hello !!");

}

};

ScheduledExecutorService service = Executors

.newSingleThreadScheduledExecutor();

service.scheduleAtFixedRate(runnable, 0, 1, TimeUnit.SECONDS);

}

}

根据以下任务要求,编写Java应用程序?

按照题目要求编写的Java程序如下

注意 请使用你的真实姓名和班级替换Test类中

创建Student对象stu时用的"张三"和"20计算机应用01班"

import java.util.Scanner;

class Student{

 private String name,classname;

 private int starnum,scorenum;

 private int[] scores;

 public void setStarNum(int n){

  this.starnum=n;

 }

 public Student(String name,String classname,int scorenum){

  this.name=name;

  this.classname=classname;

  this.scorenum=scorenum;

 }

 public String getName(){

  return this.name;

 }

 public void printStar(){

  for(int i=0;istarnum;i++){

   for(int j=0;j2*i+1;j++){

    System.out.print("*");

   }

   System.out.println();

  }

 }

 public void setScore(){

  Scanner sc=new Scanner(System.in);

  scores=new int[scorenum];

  System.out.print("请输入各科成绩:");

  for(int i=0;iscorenum;i++){

   scores[i]=sc.nextInt();

  }

 }

 public void showInfo(){

  System.out.print(name+"同学,你所在的班级是"+classname+",你各科考试成绩分别为:");

  for(int i=0;iscorenum;i++){

   if(i==scorenum-1)

    System.out.print(scores[i]);

   else

    System.out.print(scores[i]+",");

  }

  System.out.println();

 }

 public float getAvg(){

  float sum=0;

  for(int i=0;iscorenum;i++){

   sum=sum+scores[i];

  }

  return sum/scorenum;

 }

}

public class Test{

 public static void main(String[] args){

  Student stu=new Student("张三","20计算机应用01班",5);

  stu.setStarNum(4);

  stu.printStar();

  stu.setScore();

  stu.showInfo();

  if(stu.getAvg()60){

   System.out.println(stu.getName()+"是不合格学生");

  }else{

   System.out.println(stu.getName()+"是个合格学生");

  }

 }

}

关于java新建任务和Java新建工程的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2023-01-26,除非注明,否则均为首码项目网原创文章,转载请注明出处。