线程和多线程的应用.线程类Thread 和线程接口Runnable,首先创建一个线程有两种方法:(1)继承Thread 类(2)实现Runnable接口. 线程中的run()方法就是线程体.(注意的是,不能直接调用run()方法,否则将把它作为普通方法处理,而不是作为线程体运行)
线程的生命周期为: new创建线程-----start()------>Runnable可运行状态<---->Running运行中-run()-Dead死亡. Runnable可运行状态<-----Block阻塞
Running运行中----sleep()/join()--->Block阻塞
注意:线程调用start()方法启动进入Runnable可运行状态,等待cpu的调度.
改变线程的状态可用sleep(),join()等方法.,线程之间还存在优先级的问题,设置优先级的方法是setPriority(),获取优先级是getPriority().
最重要的一个问题就是:当多个线程共享数据,而都对数据进行操作时,如不作任何处理就容易造成数据操作错误,
比如银行柜台和ATM机同时对同一帐户取钱的例子.所以针对这类情况,凡是多线程共享数据的,就应该对数据进行"互斥锁"(synchronized关键字)处理:即一个线程对共享的数据进行操作时,其他线程不能访问该数据直至当前线程执行完毕.
下面是今天做的作业!第一个是用四个线程完成COPY任务,呵呵,了解了一下 flashget的多线程下载原理哈!第二个是放号器,第一行是输出双位,第二行输出单号,哎,不过这个只算是完成了,因为当不用sleep()方法时,就会出现只有一个线程运行,而另一个线程就没有机会了.不过其中的判断是当前的线程是哪个线程时发了我不少时间,一开始用==运算符,总是不行,经过调试,才发现,等号永不会成立,而直接运行else里面的程序, 才发现在引用型中要用equals()来判断内容是否相等,看来还是对这个不太清楚!这其中用到了ArrayList类,真是爽,可以用.add()加数据,还可以用直接打印出整个数列数据.第三是判断文件是否被修改过!
* 将一个500M的文件,用4个线程搬运到另外一个文件目录
里去。 '
package com.softeem.j2se.p002;
import java.io.*;
public class MoveFileRunnable implements Runnable {
private File file;
public File getFile() {
return file;
}
public File setFile(File file) {
return this.file = file;
}
public void run(){
try {
InputStream in = new FileInputStream(file);
RandomAccessFile raf = new RandomAccessFile("F:\\qq.exe", "rw");
byte[] buff = new byte[1024 * 1024];
switch (Integer.parseInt(Thread.currentThread().getName())) {
case 1:
in.skip(0);
raf.seek(0);
break;
case 2:
in.skip(file.length() / 4);
raf.seek(file.length() / 4);
break;
case 3:
in.skip(file.length() / 2);
raf.seek(file.length() / 2);
break;
case 4:
in.skip(file.length() * 3 / 4);
raf.seek(file.length() * 3 / 4);
}
int len;
while ((len = in.read(buff)) != -1) {
raf.write(buff, 0, len);
System.out.println(Thread.currentThread().getName());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
MoveFileRunnable mfr = new MoveFileRunnable();
mfr.setFile(new File("G:\\qq2006beta3.exe"));
Thread th1 = new Thread(mfr,"1");
Thread th2 = new Thread(mfr,"2");
Thread th3 = new Thread(mfr,"3");
Thread th4 = new Thread(mfr,"4");
th1.start();
th2.start();
th3.start();
th4.start();
}
}
* 写一个放号器,打印如下:
2,4,6,8,10...
1,3,7,9...
package com.softeem.j2se.theadtest2;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class NumberMark implements Runnable {
static int i = 1;
ArrayList a1 = new ArrayList();
ArrayList a2 = new ArrayList();
/**
* @param args
*/
public static void main(String[] args) {
NumberMark nm = new NumberMark();
Thread t1 = new Thread(nm, "thread1");
Thread t2 = new Thread(nm, "thread2");
t1.start();
t2.start();
}
public void run() {
while (i < 50) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (Thread.currentThread().getName().equals("thread1")) {
a1.add(i);
} else {
a2.add(i);
}
i++;
}
if (Thread.currentThread().getName().equals("thread1")) {
System.out.println(Thread.currentThread().getName() + a1);
} else {
System.out.println(Thread.currentThread().getName() + a2);
}
}
}
* 每1秒钟一次,判断指定的两个目录下的某个文件
是否被修改,如果被修改,就在Console上打印出
“File changed…”
package com.softeem.j2se.threadtest1;
import java.io.File;
public class FileChange implements Runnable {
private String path1;
private String path2;
public FileChange(String fpath1, String fpath2) {
this.path1 = fpath1;
this.path2 = fpath2;
}
public void run() {
File f1 = new File(path1);
File f2 = new File(path2);
long timestart1 = f1.lastModified();
long timestart2 = f2.lastModified();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
long timeend1 = f1.lastModified();
long timeend2 = f2.lastModified();
if (!(timestart1 == timeend1 && timestart2 == timeend2)) {
System.out.println("File Changed...");
} else {
System.out.println("File not Changed...");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
FileChange fc = new FileChange("F:\\file1\\file.txt","F:\\file2\\file.txt");
Thread thread = new Thread(fc, "thread1");
thread.start();
}
}