售票问题
多个线程访问同一个资源时会发生死锁问题。
package com.bennett.test1024;
public class TicketThread {
public static void main(String[] args) {
Ticket ticket = new Ticket();
new salMan("售票员1",ticket).start();
new salMan("售票员2",ticket).start();
new salMan("售票员3",ticket).start();
new salMan("售票员4",ticket).start();
}
}
class Ticket{
private int i = 8;
public int sal() {
System.out.println("票"+i);
i--;
return i;
}
}
class salMan extends Thread{
private String name;
Ticket ticket;
public salMan(String name, Ticket ticket) {
super();
this.name = name;
this.ticket = ticket;
}
@Override
public void run() {
synchronized (ticket) {
System.out.println(this.name+"在销售---");
ticket.sal();
}
}
}
运行结果: 加锁的结果
售票员1在销售---
票8
售票员4在销售---
票7
售票员3在销售---
票6
售票员2在销售---
票5
未加锁的结果
售票员1在销售---
售票员3在销售---
售票员4在销售---
售票员2在销售---
票8
票8
票8
票8
两线程死锁问题
两个线程共享统一资源的不同部分(A和B),线程1先使用A,线程先使用B,这样就会发生死锁。
package com.bennett.test1025;
public class TwoDeadThread {
static Object o1 = new Object();
static Object o2 = new Object();
public static void main(String[] args) {
Thread s1 = new Thread(new Lock1());
Thread s2 = new Thread(new Lock2());
s1.start();
s2.start();
}
}
class Lock1 implements Runnable{
@Override
public void run() {
synchronized (TwoDeadThread.o1){
try {
System.out.println("get o1");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (TwoDeadThread.o2){
System.out.println("get o2");
}
}
}
}
class Lock2 implements Runnable{
@Override
public void run() {
synchronized (TwoDeadThread.o2){
try {
System.out.println("get o2");
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (TwoDeadThread.o1){
System.out.println("get o1");
}
}
}
}
1a2b3c问题
package com.bennett.test1025;
import java.util.Random;
public class ConcurretPrintTest {
static class ThreadA extends Thread {
private Object flag;
public ThreadA(Object flag) {
this.flag = flag;
}
private String[] array = "1,2,3".split(",");
@Override
public void run() {
synchronized (flag) {
for (String string : array) {
System.out.print(string + ",");
try {
Thread.sleep(new Random().nextInt(50));
} catch (InterruptedException e) {
e.printStackTrace();
}
flag.notifyAll();
try {
flag.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
static class ThreadB extends Thread{
private Object flag;
private String[] array="a,b,c".split(",");
public ThreadB(Object flag) {
this.flag = flag;
}
@Override
public void run() {
synchronized(flag) {
try {
flag.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
for (String string : array) {
System.out.print(string+",");
try {
Thread.sleep(new Random().nextInt(50));
} catch (InterruptedException e) {
e.printStackTrace();
}
flag.notify();
try {
flag.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
Object xiHaoDeng=new Object();
new ThreadB(xiHaoDeng).start();
new ThreadA(xiHaoDeng).start();
}
}
多线程考拷贝文件
package com.bennett.test1012;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.CountDownLatch;
public class CopyFilesByRunnableDemo extends Thread {
private final int NUMBER = 1024;
CountDownLatch downLatch;
RandomAccessFile oldFile;
RandomAccessFile newFile;
String oldRUL;
String newRUL;
long start;
long end;
public CopyFilesByRunnableDemo(String oldRUL, String newRUL, long start, long end, CountDownLatch downLatch) {
this.oldRUL = oldRUL;
this.newRUL = newRUL;
this.start = start;
this.end = end;
this.downLatch = downLatch;
}
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + "线程启动了...");
long len = end - start;
long length = 0;
oldFile = new RandomAccessFile(oldRUL, "rw");
newFile = new RandomAccessFile(newRUL, "rw");
oldFile.seek(start);
newFile.seek(start);
byte[] bytes = new byte[NUMBER];
while (true) {
if ((len - length) <= NUMBER) {
oldFile.read(bytes, 0, (int) (len - length));
newFile.write(bytes, 0, (int) (len - length));
break;
} else {
oldFile.read(bytes);
newFile.write(bytes);
length += NUMBER;
}
}
oldFile.close();
newFile.close();
downLatch.countDown();
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.bennett.test1012;
import java.io.File;
import java.util.concurrent.CountDownLatch;
public class CopyFilesTest {
public static void main(String[] args) {
String oldURL = "E:/BaiduNetdiskDownload/软件设计师历年上午试卷.docx";
String newURL = "E:/file2.docx";
File oldFile = new File(oldURL);
long lengthFile = oldFile.length();
int threadCount = 4;
long meanLen = lengthFile / threadCount;
long start;
long end;
long startCurrent = System.currentTimeMillis();
CountDownLatch downLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
if (i == threadCount - 1L) {
start = i * meanLen;
end = lengthFile;
new CopyFilesByRunnableDemo(oldURL, newURL, start, end, downLatch).start();
} else {
start = i * meanLen;
end = start + meanLen - 1L;
new CopyFilesByRunnableDemo(oldURL, newURL, start, end, downLatch).start();
}
}
try {
downLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
long entCurrent = System.currentTimeMillis();
System.out.println("文件拷贝使用时间(毫秒):"+(entCurrent - startCurrent));
}
}
拷贝文件
package com.bennett.test1008;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class FileCopy {
public static void main(String[] args) {
if(args.length!=2) {
System.out.println("请指定源文件和目标文件");
return;
}
if(args[0]==null) {
System.out.println("请指定源文件");
return;
}
File src=new File(args[0]);
if(!src.exists()) {
System.out.println("源文件不存在");
return;
}
if(args[1]==null) {
System.out.println("请指定目标文件");
return;
}
File dest=new File(args[1]);
if(dest.getParentFile()!=null && !dest.getParentFile().exists()) {
System.out.println(dest.getAbsolutePath());
dest.getParentFile().mkdirs();
}
try(
InputStream is=new FileInputStream(src);
OutputStream os=new FileOutputStream(dest,false);
){
int i=0;
byte[] buffer=new byte[1024];
while((i=is.read(buffer))!=-1) {
os.write(buffer,0,i);
System.out.println(i);
os.flush();
}
os.close();;
is.close();;
System.out.println("复制成功");
}catch (Exception e) {
}
}
}
|