1、编写两个线程,一个打印a-z,一个打印A-Z。
//1、编写两个线程,一个打印a-z,一个打印A-Z。
public class MyThread1 extends Thread {
@Override
public void run() {
// for (int i = 97; i <123; i++) {
// char a=(char) i;
// System.out.println(a);
// }
for (char c = 'a'; c < 'z'; c++) {
System.out.println(c);
}
}
}
package week3.day4.zy1;
public class MyThread2 extends Thread{
@Override
public void run() {
for (int i = 65; i < 91; i++) {
char a=(char) i;
System.out.println(a);
}
}
}
package week3.day4.zy1;
public class zy1 {
public static void main(String[] args) {
MyThread1 t1=new MyThread1();
MyThread2 t2=new MyThread2();
t1.start();
t2.start();
}
}
2.编写一个有两个线程的程序,第一个线程用来计算2~100000之间的奇数的个数,第二个线程用来计算100000~200000之间的偶数的个数,最后输出结果。
public class Thread1 extends Thread{
int num1=0;
@Override
public void run() {
for (int i = 2; i < 1000001; i++) {
if(i%2==0){
num1++;
}
}
System.out.println("偶数:"+num1);
}
}
public class Thread2 extends Thread{
int num2=0;
@Override
public void run() {
for (int i = 2; i < 1000001; i++) {
if(i%2!=0){
num2++;
}
}
System.out.println("偶数:"+num2);
}
}
public class zy2 {
public static void main(String[] args) {
Thread1 t1=new Thread1();
Thread2 t2=new Thread2();
t1.start();
t2.start();
}
}
3、使用多线程,模拟龟兔赛跑的场景。
public class Rabbit extends Thread{
@Override
public void run() {
int length=0;
while(true){
//跑
length+=3;
try {
Thread.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(length>=100){
break;
}
}
System.out.println("rabbit end");
}
}
package week3.day4.lx;
public class Tortoise extends Thread{
@Override
public void run() {
int length=0;
while(true){
//跑
length+=1;
if(length>=100){
break;
}
}
System.out.println("tortoise end");
}
}
package week3.day4.lx;
public class Test3 {
public static void main(String[] args) {
System.out.println("预备,跑");
Tortoise t=new Tortoise();
t.start();
Rabbit r=new Rabbit();
r.start();
}
}
4.使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
package week3.day4.zy4;
import java.io.*;
public class Thread1 extends Thread{
private File oldFile;
private File newFile;
public Thread1(String oldFile, String newFile) {
this.oldFile = new File(oldFile);
this.newFile = new File(newFile);
}
@Override
public void run() {
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(oldFile);
out=new FileOutputStream(newFile);
double length=oldFile.length();
double now=0;
int a=1;
byte[] b=new byte[1];
int i=in.read(b);
while (i!=-1){
now+=i;
out.write(b);
i=in.read(b);
if (now>length*(a*0.1)){
System.out.println("文件复制了"+oldFile.getName()+a*10+"%");
a++;
}
}
System.out.println(oldFile.getName()+"复制完成");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package week3.day4.zy4;
import java.io.File;
//使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。
// 例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
public class zy4 {
public static void main(String[] args) {
Thread1 t1=new Thread1("C:\\Users\\lenovo\\Desktop\\1.txt","C:\\Users\\lenovo\\Desktop\\2.txt");
Thread1 t2=new Thread1("C:\\Users\\lenovo\\Desktop\\3.txt","C:\\Users\\lenovo\\Desktop\\4.txt");
t1.start();
t2.start();
}
}