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つのスレッドでプログラムを作成します。最初のスレッドは2〜100,000の素数の数を計算するために使用され、2番目のスレッドは100,000〜200,000の素数の数を計算するために使用され、最終的に結果を出力します。 。
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つのスレッドを設計します。そのうちの2つのスレッドはjに対して毎回1ずつ増加し、他の2つのスレッドは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);
}
}