List<Integer> list = new ArrayList<>();
,用这个数组来存放获取到的票就可以很好的显示结果:package 任务十__多线程.售票;
import java.util.ArrayList;
import java.util.List;
public class TestDemo {
//定义售票线程类(也就是窗口)
public static class Station extends Thread{
//构造方法给线程名字赋值
public Station(String name) {
super(name);
}
//票数要静态定义
static int tick=50;
//静态钥匙
static Object ob ="key"; //值是任意的
//重写run方法,实现售票操作
@Override
public void run() {
List<Integer> list = new ArrayList<>();
while (tick>0) {
synchronized(ob) { //必须使用一个同步锁,进去的人会把钥匙拿在手上,出来后才能交出钥匙
if (tick>0) {
System.out.printf("%s卖出了第%d张票 \n",getName(),tick);
list.add(tick);
tick--;
}else {
System.out.printf("%s:票已售空 \n",getName());
}
}
try {
sleep((int)(Math.random()*3000)+1); //随机休息1-3000ms
}catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.printf("%s 销售情况: %s \n",getName(),list.toString());
}
}
public static void main(String[] args) {
//实例化站台对象,并为每一个站台取名字(8个线程窗口一起卖5张票)
for (int i=1; i<=8; i++) {
String sName="窗口" + String.valueOf(i);
Station Station = new Station(sName);
Station.start();
}
}
}
System.out.println(getName()+"正在卖票");
package 任务十__多线程.售票;
/** * @author ${范涛之} * @Description * @create 2021-11-24 23:43 */
class SaleThread extends Thread {
/** * 使用静态成员变量作为100张票的保存变量,是一个共享资源。 */
private static int tickets = 20;
public SaleThread(String name){
super(name);
}
@Override
public void run() {
// 完成售票过程
while (true) {
System.out.println(getName()+"正在卖票");
synchronized (this) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (tickets > 0) {
System.out.println(Thread.currentThread().getName() + "售出了" + tickets + "张票");
tickets--;
} else {
System.out.println(Thread.currentThread().getName() + "售罄!!!");
break;
}
}
}
}
}
public class Demo {
public static void main(String[] args) {
new SaleThread("范涛之").start();
new SaleThread("张荣康").start();
new SaleThread("秦舒下").start();
}
}
因为线程即使执行完System.out.println(getName() + “抢到了第” + ticket + “火车票”);但是还没来得及打印,就被别的线程抢走了,等别的线程打印了,他可能抢到执行时间,才会打印,所以,在上面加一行System.out.println(getName() + “正在抢票…”);系统打印就能正确显示。
程序并没有错误,只是System.out.println跟不上线程的速度。
List<File> fileList = new ArrayList<File>();
File file = new File(fileDir);
File[] files = file.listFiles();
if (files==null){
return;
}
for (File f :files){
if (f.isFile()){
fileList.add(f);
}else if (f.isDirectory()){
System.out.println(f.getAbsolutePath());
test(f.getAbsolutePath()); //递归调用
}
}
for (File f1 :fileList){
System.out.println(f1.getName());
}
public static void main(String[] args) {
test("C:/");
}
package 任务11IO流;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/** * @author ${范涛之} * @Description * @create 2021-11-25 12:31 */
public class Test5 {
private static void test(String fileDir){
List<File> fileList = new ArrayList<File>();
File file = new File(fileDir);
File[] files = file.listFiles();
if (files==null){
return;
}
for (File f :files){
if (f.isFile()){
fileList.add(f);
}else if (f.isDirectory()){
System.out.println(f.getAbsolutePath());
test(f.getAbsolutePath()); //递归调用
}
}
for (File f1 :fileList){
System.out.println(f1.getName());
}
}
public static void main(String[] args) {
test("C:/");
}
}
package 任务11IO流;
import java.io.*;
public class Test6{
private static void copyFileUsingFileStreams(String source, String dest) throws IOException {
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(source);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf))>0){
output.write(buf,0,bytesRead);
}
}finally {
input.close();
output.close();
}
}
public static void main(String[] args) throws IOException {
copyFileUsingFileStreams("H:\\bilbil\\07.mp4","F:\\shipin\\007.mp4");
}
}
package 任务11IO流;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/** * @author ${范涛之} * @Description * @create 2021-11-25 20:56 */
public class Test7 {
private static void copyFileUsingFileChannels(String sourse,String dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(sourse).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel,0,inputChannel.size());
}finally {
inputChannel.close();
outputChannel.close();
}
}
public static void main(String[] args) throws IOException {
copyFileUsingFileChannels("H:\\bilbil\\毒液2.mp4","F:\\shipin\\复制版毒液2.mp4");
}
}
package 任务11IO流;
import java.io.*;
/** * @author ${范涛之} * @Description * @create 2021-11-25 21:48 */
public class Test8 {
private static void copyFileUsingFileStreams(String source, String dest) throws IOException {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader(source);
writer = new FileWriter(dest);
char[] chs = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(chs)) > 0) {
writer.write(chs, 0, bytesRead);
}
} finally {
reader.close();
writer.close();
}
}
public static void main(String[] args) throws IOException {
copyFileUsingFileStreams("H:\\bilbil\\毒液2.mp4","F:\\shipin\\哈哈哈毒液2.mp4");
}
}
package 任务11IO流;
import java.io.*;
/** * @author ${范涛之} * @Description * @create 2021-11-25 21:48 */
public class Test8 {
private static void copyFileUsingFileStreams(String source, String dest) throws IOException {
FileReader reader = null;
FileWriter writer = null;
try {
reader = new FileReader(source);
writer = new FileWriter(dest);
char[] chs = new char[1024];
int bytesRead;
while ((bytesRead = reader.read(chs)) > 0) {
writer.write(chs, 0, bytesRead);
}
} finally {
reader.close();
writer.close();
}
}
public static void main(String[] args) throws IOException {
copyFileUsingFileStreams("H:\\bilbil\\我爱佳佳.txt","F:\\shipin\\我超级爱佳佳.txt");
}
}
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/justleavel/article/details/121534754
内容来源于网络,如有侵权,请联系作者删除!
List<Integer> list = new ArrayList<>();
,用这个数组来存放获取到的票就可以很好的显示结果:System.out.println(getName()+"正在卖票");