1、使用多线程,模拟龟兔赛跑的场景。
public class Gui implements Runnable {
@Override
public void run() {
Thread.currentThread().setName("乌龟:");
for (int i = 0; i <1000 ; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
}
}
public class Tu implements Runnable{
public void run() {
Thread.currentThread().setName("兔子:");
for (int i = 0; i <1000 ; i++) {
System.out.println(Thread.currentThread().getName()+i);
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
Gui gui = new Gui();
Tu tu = new Tu();
Thread thread = new Thread(gui);
Thread thread1 = new Thread(tu);
thread.start();
thread1.start();
System.out.println("龟兔赛跑开始");
}
}
2、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的奇数的个数,第二个线程用来计算100000~200000之间的偶数的个数,最后输出结果。
public class Threadone extends Thread {
@Override
public void run() {
super.run();
int sum=0;
for (int i = 2; i <100000 ; i++) {
if (i%2!=0){
sum++;
}
}
System.out.println("2-100000中的奇数个数为:"+sum);
}
}
public class Threadtwo extends Thread {
@Override
public void run() {
super.run();
int sum=0;
for (int i = 100000; i <200000 ; i++) {
if (i%2==0){
sum++;
}
}
System.out.println("1000000-200000中的偶数个数为:"+sum);
}
}
public class Test {
public static void main(String[] args) {
Threadone threadone = new Threadone();
Threadtwo threadtwo = new Threadtwo();
threadone.start();
threadtwo.start();
}
}
3、使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
import java.io.*;
import java.math.BigDecimal;
public class MyThread extends Thread {
private File oldfile;
private File newfile;
public MyThread(String oldfilepath, String newfilepath) {
this.oldfile = new File(oldfilepath);
this.newfile = new File(newfilepath);
}
@Override
public void run() {
super.run();
FileInputStream fileInputStream =null;
FileOutputStream fileOutputStream =null;
try {
fileInputStream= new FileInputStream(oldfile);
fileOutputStream= new FileOutputStream(newfile);
byte[] bytes = new byte[1024];
int read = fileInputStream.read(bytes);
double jdt=(double)oldfile.length();
int jd=0;
while (read!=-1){
jd=jd+read;
BigDecimal divide =
new BigDecimal(jd).divide(new BigDecimal(jdt), 2, BigDecimal.ROUND_HALF_UP);
System.out.println(oldfile.getName()+"复制进度"+divide.multiply(new BigDecimal(100))+"%");
fileOutputStream.write(bytes,0,read);
read = fileInputStream.read(bytes);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.IOException;
public class MainThreadTest {
public static void main(String[] args) throws IOException {
MyThread myThread = new MyThread("src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp1\\3.jpg", "src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp2\\3.jpg");
MyThread myThread1 = new MyThread("src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp1\\4.jpg", "src\\ParcticeDiwuzhou\\lx0812\\lx\\lx03\\tp2\\4.jpg");
myThread.start();
myThread1.start();
}
}
4、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。不考虑线程的安全性。
public class J {
public static int j=0;
}
public class Ixc1 extends Thread{
@Override
public void run() {
super.run();
for (int i = 0; i < 100; i++) {
System.out.println("加:"+J.j++);
}
}
}
public class Xc1 extends Thread {
@Override
public void run() {
super.run();
for (int i = 0; i <100 ; i++) {
System.out.println("减:"+J.j--);
}
}
}
public class Test {
public static void main(String[] args) {
Ixc1 ixc1 = new Ixc1();
Ixc1 ixc11 = new Ixc1();
Xc1 xc1 = new Xc1();
Xc1 xc11 = new Xc1();
ixc1.start();
ixc11.start();
xc1.start();
xc11.start();
}
}