java一个多线程的经典例子
import java.io.*;
//多线程编程
public class MultiThread
{
public static void main(String args[])
{
System.out.println("我是主线程!");
//下面创建线程实例thread1
ThreadUseExtends thread1=new ThreadUseExtends();
//创建thread2时以实现了Runnable接口的THhreadUseRunnable类实例为参数
Thread thread2=new Thread(new ThreadUseRunnable(),"SecondThread");
thread1.start();//启动线程thread1使之处于就绪状态
//thread1.setPriority(6);//设置thread1的优先级为6
//优先级将决定cpu空出时,处于就绪状态的线程谁先占领cpu开始运行
//优先级范围1到10,MIN_PRIORITY,MAX_PRIORITY,NORM_PAIORITY
//新线程继承创建她的父线程优先级,父线程通常有普通优先级即5NORM_PRIORITY
System.out.println("主线程将挂起7秒!");
try
{
Thread.sleep(7000);//主线程挂起7秒
}
catch (InterruptedException e)
{
return;
}
System.out.println("又回到了主线程!");
if(thread1.isAlive())
{
thread1.stop();//如果thread1还存在则杀掉他
System.out.println("thread1休眠过长,主线程杀掉了thread1!");
}
else
System.out.println("主线程没发现thread1,thread1已醒顺序执行结束了!");
thread2.start();//启动thread2
System.out.println("主线程又将挂起7秒!");
try
{
Thread.sleep(7000);//主线程挂起7秒
}
catch (InterruptedException e)
{
return;
}
System.out.println("又回到了主线程!");
if(thread2.isAlive())
{
thread2.stop();//如果thread2还存在则杀掉他
System.out.println("thread2休眠过长,主线程杀掉了thread2!");
}
else
System.out.println("主线程没发现thread2,thread2已醒顺序执行结束了!");
System.out.println("程序结束按任意键继续!");
try
{
System.in.read();
}
catch (IOException e)
{
System.out.println(e.toString());
}
}//main
}/
相关文档:
这都是我学习JAVA亲身经历的心得,今天花了一个多小时总结出来希望对大家有一定的帮助。如果如总结不妥,请指出和批评!为学习JAVA的初学者铺条学习JAVA的道路
首先大家要明确一点,外面招聘JAVA的,主要是指的J2EE,也就是BS结构(浏览器和服务器结构)JAVA的之所以火起来真是因为BS结构,并不在CS结构,所以那种大型网游 ......
Java中的23种设计模式
1、工厂模式:客户 ......
public ActionForward backUpAction(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
&n ......
通过反射创建新类示例的两种方式及比较
作者BLOG:http://blog.csdn.net/fenglibing
通过反射创建新的类示例,有两种方式:
Class.newInstance()
Constructor.newInstance()
以下对两种调用方式给以比较说明:
l Class.newInstance() 只能 ......
前不久写了个下载文件的方法。经过一段时间在程序中的运用,发现了几个问题。修正一下:
public static File saveToFiles(String destUrl,String path) throws IOException {
final int BUFFER_SIZE = 4096;
&nb ......