1.使用多线程,模拟龟兔赛跑的场景。
package lianxi1;
class Rabbit extends Thread{
int i;
@Override
public void run() {
for ( i=0;i<=1000;i+=100)
{
System.out.println("兔子跑了"+i+"米");
if (i==1000) {
System.out.println("兔子跑完了全程");
}
}
}
}
class Tor extends Thread{
int i=0;
@Override
public void run() {
for (int i=0;i<=1000;i+=50)
{
System.out.println("乌龟跑了"+i+"米");
if (i==1000)
{
System.out.println("乌龟跑完了全程");
}
}
}
}
public class Race {
public static void main(String[] args) {
Rabbit r=new Rabbit();
Tor t=new Tor();
r.start();
t.start();
}
}
2、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果。
package lianxi2;
public class ThreadPractice2 {
public static void main(String[] args) {
Thread thread1 = new Thread(new Runnable() {
@Override
public synchronized void run() {
int count1 = 0;
for (int i = 2; i < 100000; i++){
if(isPrime(i)){
count1++;
}
}
System.out.println(Thread.currentThread().getName() + ": 0-100000有" + count1 + "个质数");
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public synchronized void run() {
int count2 = 0;
for (int j = 100000; j < 200000; j++){
if(isPrime(j)){
count2++;
}
}
System.out.println(Thread.currentThread().getName() + ": 0-200000有" + count2 + "个质数");
}
});
thread1.start();
thread2.start();
}
public static boolean isPrime(long m) {
if(m > 2 && (m & 1) == 0){
return false;
}
for(int n = 3; n * n <= m; n += 2){
if (m % n == 0) {
return false;
}
}
return true;
}
}
3、使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
package LianXi3;
import java.io.*;
import java.text.DecimalFormat;
public class CopyFiles extends Thread {
//要读取的源文件
private File src;
//要写入的目标文件
private File dest;
public CopyFiles(String src, String dest) {
this.src = new File(src);
this.dest = new File(dest);
}
@Override
public void run() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(src);
fos = new FileOutputStream(dest);
byte[] b = new byte[1024];
int length = 0;
//获取源文件大小
long len = src.length();
//已经复制文件的字节数
double temp = 0;
//数字格式化,显示百分比
DecimalFormat decimalFormat = new DecimalFormat("##.00%");
while ((length = fis.read(b)) != -1) {
//输出字节
fos.write(b, 0, length);
//获取以输出的字节大小转化成百分比
temp += length;
double d = temp / len;
System.out.println(src.getName() + "文件已复制" + decimalFormat.format(d));
}
System.out.println(src.getName() + "文件已经复制完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
package LianXi3;
public class ThreadPractice3 {
public static void main(String[] args) {
CopyFiles cfs1 = new CopyFiles("D:\\aaa.txt","D:\\ccc.txt");
CopyFiles cfs2 = new CopyFiles("D:\\bbb.txt","D:\\ddd.txt");
cfs1.start();
cfs2.start();
}
}
4、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。考虑到线程的安全性写出程序。
package lianxi4;
public class ThreadPractice4{
/**
* 对Operation类中的 加、减 方法添加锁,
* 当一个线程对 j 进行操作时,会加锁,其他线程要对 j 操作需要等待当前对象释放锁才能进行。
*
* 如果不加synchronized,会出现执行一个线程的同时,另一个线程也执行,导致错误结果(线程不安全)
* @param args
*/
public static void main(String[] args) {
final Operation operation = new Operation();
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
operation.add();
}
});
Thread thread2 = new Thread(new Runnable() {
@Override
public void run() {
operation.add();
}
});
Thread thread3 = new Thread(new Runnable() {
@Override
public void run() {
operation.sub();
}
});
Thread thread4 = new Thread(new Runnable() {
@Override
public void run() {
operation.sub();
}
});
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
package lianxi4;
public class Operation {
private int j = 0;
//加运算
public synchronized void add(){
j++;
System.out.println("add");
System.out.println(Thread.currentThread().getName() + " : j=" + j);
}
//减运算
public synchronized void sub(){
j--;
System.out.println("sub");
System.out.println(Thread.currentThread().getName() + " : j=" + j);
}
}