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
}/
相关文档:
从网络摘抄到的解决方法——
解决方法二:
连接mysql时(无论在从mysql读还是取数据的情况),指定使用的编码方式为utf-8,具体代码如下
//装载mysql-jdbc驱动
Class.forName("com.mysql.jdbc.Driver" ......
JAVA Calendar详解
(在文章的最后,将会介绍Date类,如果有兴趣,可以直接翻到最后去阅读)
究竟什么是一个 Calendar 呢?中文的翻译就是日历,那我们立刻可以想到我们生活中有阳(公)历、阴(农)历之分。它们的区别在哪呢?
比如有:
月份的定义 - 阳`(公)历 一年12 个月,每个月的天数各不同;阴(农)历,每个月固定28 ......
通过反射创建新类示例的两种方式及比较
作者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 ......