1、用共享资源的方式实现 生产者–仓库–消费者的模式(注意线程安全)
线程通信
public class Test4 {
public static void main(String[] args) {
Storage s=new Storage();
Worker w=new Worker();
w.setName("生产者");
w.s=s;
Customer c=new Customer();
c.s=s;
c.setName("消费者");
w.start();
c.start();
}
}
class Worker extends Thread{
Storage s;
public void run(){
while (true){
s.addStore();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Customer extends Thread{
Storage s;
public void run(){
while (true) {
s.minusStore();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Storage{
public int stores=10;
public synchronized void addStore(){
if (stores>=20){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
stores++;
System.out.println(Thread.currentThread().getName()+"生产产品,当前库存为:"+stores);
notifyAll();
}
}
public synchronized void minusStore(){
if (stores<=0){
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}else {
stores--;
System.out.println(Thread.currentThread().getName()+"消费者进行消费,当前库存为:"+stores);
notifyAll();
}
}
}
2、使用多线程实现多个文件同步复制功能,并在控制台显示复制的进度,进度以百分比表示。例如:把文件A复制到E盘某文件夹下,在控制台上显示“XXX文件已复制10%”,“XXX文件已复制20%”……“XXX文件已复制100%”,“XXX复制完成!”
PS:DecimalFormat中的format方法可以将小数转成百分数
public class Test3 extends Thread {
private File file;
private File target;
private int len;//每次读取的长度
public Test3(File file, File target,int len) {
this.file = file;
this.target = target;
this.len=len;
}
@Override
public void run() {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
fis=new FileInputStream(file);
fos=new FileOutputStream(target);
byte[] b=new byte[len];
int tmp;
int total=fis.available();
double loaded=0;
while ((tmp=fis.read(b))!=-1){
fos.write(tmp);
loaded+=tmp;
double per=loaded/total;
// double percent=(int)(per*10000)/100.0;
// System.out.println(file.getName()+"已读取"+percent+"%");
DecimalFormat df=new DecimalFormat("##.##%");
System.out.println(file.getName()+"已读取"+df.format(per));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Test {
public static void main(String[] args) {
Test3 t1=new Test3(new File("E:/work/1.txt"),new File("E:/PlayCards/1.txt"),1);
Test3 t2=new Test3(new File("E:/work/2.txt"),new File("E:/PlayCards/2.txt"),1);
Test3 t3=new Test3(new File("E:/work/3.txt"),new File("E:/PlayCards/3.txt"),1);
t1.start();
t2.start();
t3.start();
}
}
3、设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。考虑到线程的安全性写出程序
public synchronized void run() {
int j=0;
if (Thread.currentThread().getName().equals("线程1") || Thread.currentThread().getName().equals("线程2")){
j++;
}
if (Thread.currentThread().getName().equals("线程3") || Thread.currentThread().getName().equals("线程4")){
j--;
}
System.out.println(Thread.currentThread().getName()+":"+j);
}
public static void main(String[] args) {
HomeWork t=new HomeWork();
Thread t1=new Thread(t,"线程1");
Thread t2=new Thread(t,"线程2");
Thread t3=new Thread(t,"线程3");
Thread t4=new Thread(t,"线程4");
t1.start();
t2.start();
t3.start();
t4.start();
}
4、编写一个有两个线程的程序,第一个线程用来计算2~100000之间的素数的个数,第二个线程用来计算100000~200000之间的素数的个数,最后输出结果
public void run() {
for (int i = 100000; i <= 200000; i++) {
boolean is=true;
for (int j = 2; j < i; j++) {
if (i%j==0){
is=false;
break;
}
}
if (is)count++;
}
System.out.println(Thread.currentThread().getName()+":100000到200000之间有"+count+"个素数");
}
public void run() {
for (int i = 2; i <= 100000; i++) {
boolean is=true;
for (int j = 2; j < i; j++) {
if (i%j==0){
is=false;
break;
}
}
if (is)count++;
}
System.out.println(Thread.currentThread().getName()+":2到100000之间有"+count+"个素数");
}
public static void main(String[] args) {
Abc r=new Abc();
Bbb r2=new Bbb();
Thread t1=new Thread(r,"线程1");
Thread t2=new Thread(r2,"线程2");
t1.start();
t2.start();
}
因篇幅问题不能全部显示,请点此查看更多更全内容
怀疑对方AI换脸可以让对方摁鼻子 真人摁下去鼻子会变形
女子野生动物园下车狼悄悄靠近 后车司机按喇叭提醒
睡前玩8分钟手机身体兴奋1小时 还可能让你“变丑”
惊蛰为啥吃梨?倒春寒来不来就看惊蛰
男子高速犯困开智能驾驶出事故 60万刚买的奔驰严重损毁