文章目录
使用多线程,模拟龟兔赛跑的场景。
public class RabbitAndTurtle extends Thread {
public int distance = 100;
static boolean flag = true;
public int predistance = 0;
@Override
public void run() {
double ran = Math.random();
String name = Thread.currentThread().getName();
while (flag){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (name.equals("乌龟")) {
if (Math.random() < 1) {
predistance += 1;
System.out.println(name + "我跑了:" + predistance + "米");
if (predistance == 100) {
System.out.println("=================乌龟赢了=================");
flag = false;
break;
}
}
try {
sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (name.equals("兔子")) {
if (Math.random() < 0.3) {
predistance += 2;
System.out.println(name + "我跑了:" + predistance + "米");
if (predistance == 100) {
System.out.println("=================兔子赢了=================");
flag = false;
break;
}
}
}
try {
sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public class Race {
public static void main(String[] args) {
Thread rabbit = new RabbitAndTurtle();
Thread turtle = new RabbitAndTurtle();
rabbit.setName("兔子");
turtle.setName("乌龟");
rabbit.start();
turtle.start();
}
}
参考文章:https://blog.csdn.net/Luciferooorz/article/details/77074285
编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果。
public class test extends Thread{
int i,j,x = 0;
test(int m,int n){
this.i = m;
this.j = n;
}
public void run(){
int p,q;
p=0;q=0;
for(int m=i;m<=j;m++) {
for(int h=1;h<=m;h++) {
q=m%h;
if(q==0)p=p+1;
}
if(p==2) {
x=x+1;
}
p=0;
}
System.out.println("输出"+i+"到"+j+"之间的质数个数:"+x);
}
public static void main(String[] args) {
test thread1 = new test(2,100000);
test thread2 = new test(100000,200000);
thread1.start();
thread2.start();
}
}
使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
public class copyfile extends Thread{
public File older;// 源文件路径
public File newer;// 复制目标路径
public copyfile(String older, String newer) {
this.older = new File(older);
this.newer = new File(newer);
}
@Override
public void run() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(older);
fos = new FileOutputStream(newer);
byte[] b = new byte[1024];// 声明一个字节数组,每次读取的数据存到该字节数组里
int length = 0;// 返回每次读取的数据长度
long len = older.length();// 获取源文件的长度
double temp = 0;
DecimalFormat df = new DecimalFormat("##%");
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
while ((length = fis.read(b)) != -1) {
fos.write(b, 0, length);// 把每次读取的内容,输出到目标路径文件中
temp += length;// 把每次读取的数据长度累加
double d = temp / len;// 计算出已经读取的长度占文件总长度的比率
int jd = (int) d;
if (jd % 10 == 0) {
System.out.println(older.getName() + "文件复制了:" + df.format(d));
}
}
System.out.println(older.getName() + "复制完毕!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class test {
public static void main(String[] args) {
copyfile cf1 = new copyfile("E:\\test\\a.CHM", "F:\\test\\a.CHM");
copyfile cf2 = new copyfile("E:\\test\\b.CHM", "F:\\test\\b.CHM");
copyfile cf3 = new copyfile("E:\\test\\c.CHM", "F:\\test\\c.CHM");
cf1.start();
cf2.start();
cf3.start();
}
}
设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。考虑到线程的安全性写出程序。
public class ManyThreads2 {
private int j = 0;
public synchronized void inc() {
j++;
System.out.println(Thread.currentThread().getName() + "inc" + j);
}
public synchronized void dec() {
j--;
System.out.println(Thread.currentThread().getName() + "dec" + j);
}
}
public class MyTest {
private ManyThreads2 many = new ManyThreads2();
public static void main(String[] args) {
// TODO Auto-generated method stub
MyTest myTest = new MyTest();
myTest.test();
}
public void test() {
for (int i = 0; i < 2; i++) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 20; i++) {
many.inc();
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
for (int i = 0; i < 20; i++) {
many.dec();
}
}
}).start();
}
}
}