在java中建两个线程,一个每隔一秒运算一次,另一个每隔两秒输出一次,两个线程同时运行. 新手上路,谢谢大家
1个回答
展开全部
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author coolboy
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
myThread a = new myThread();
a.start();
while (true)
{
try
{
Thread.sleep(2000);
} catch (InterruptedException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("this is thread2");
}
// TODO code application logic here
}
}
class myThread extends Thread
{
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException ex)
{
Logger.getLogger(myThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("this is thread1");
}
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication2;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author coolboy
*/
public class Main
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
myThread a = new myThread();
a.start();
while (true)
{
try
{
Thread.sleep(2000);
} catch (InterruptedException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("this is thread2");
}
// TODO code application logic here
}
}
class myThread extends Thread
{
public void run()
{
while (true)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException ex)
{
Logger.getLogger(myThread.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("this is thread1");
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |