那write操作抽取到单独的方法,然后同步那个方法....
解决方案 »
- 遍历Vector,Vector的值是正确的,但取出错误的element
- StreamCorruptedException,晕死了
- 自然语言处理
- 紧急求救:eclipse工程下bin文件夹里我删除了一个.class引发的血案
- java小程序—总共创建了多少个对象! 最终使用到了几个?
- 最近要用JAVA做个泡泡堂双人单机小游戏,目前正在写设计文档,模块部分要怎么写啊?
- 请问大家在JAVA中怎么样声明标签数组???
- 真正java高手请进………………………………………………!!
- 执行过程错误。(在线等待,急!!!)
- 如何统计微信朋友圈分享的链接的被浏览数量?
- java对象内存地址的问题??????
- JNI使用入门问题,请指点
还有同步write就行了吗?
public class Testaaa extends Thread{
private int i = 0;
public Testaaa(int i){
this.i = i;
}
public void run(){
try{
// System.out.println("1111111");
FileOutputStream out = new FileOutputStream("C:\\test.txt",true);
out.write(("start write---------" + i + "\r\n").getBytes());
out.flush();
out.close();
Testd t=Testd.instance();
t.mA(i);
Thread.sleep(1000);
out = new FileOutputStream("C:\\test.txt",true);
out.write(("end write---------" + i + "\r\n").getBytes());
out.flush();
out.close();
}catch(Exception e){
System.out.println("TestObject--------------" + e.getMessage());
}
}
public static void main(String[] args){
int i = 0;
while(i<9){
new Testaaa(i++).start();
}
}
}class Testd{
private static Testd t =null;
private Testd(){
}
synchronized public static Testd instance(){
if(t==null){
t=new Testd();
}
return t;
}
public void mA(int i){
try{
Thread.sleep(5000);
FileOutputStream out = new FileOutputStream("C:\\test.txt",true);
out.write(("Test.mA() write---------" + i + "\r\n").getBytes());
out.flush();
out.close();
}catch(Exception e){
System.out.println("Test--------------" + e.getMessage());
}
}
}
这样可以吗?先前线程会一直运行,不会结束,结果那个文件会被写的无穷大
import java.io.*;
public class TestObject extends Thread{
private int i = 0;
public TestObject(int i){
this.i = i;
}
public void run(){
try{
FileOutputStream out = new FileOutputStream("C:/test.txt",true);
Test.t.w(out, "start write---------" + i + "\r\n");
out.flush();
out.close();
Test.t.mA(i);
Thread.sleep(1000);
out = new FileOutputStream("C:/test.txt",true);
Test.t.w(out, "end write---------" + i + "\r\n");
out.flush();
out.close();
}catch(Exception e){
System.out.println("TestObject--------------" + e.getMessage());
}
}
public static void main(String[] args){
int i = 0;
while(true){
new TestObject(i++).start();
}
}
}class Test{
public static Test t = new Test();
public synchronized void w(FileOutputStream out, String str){
try{
out.write(str.getBytes());
}catch(Exception e){
System.out.println("Test.w--------------" + e.getMessage());
}
}
public void mA(int i){
try{
Thread.sleep(5000);
FileOutputStream out = new FileOutputStream("C:/test.txt",true);
w(out, "Test.mA() write---------" + i + "\r\n");
out.flush();
out.close();
}catch(Exception e){
System.out.println("Test.mA--------------" + e.getMessage());
}
}
}
import java.io.FileOutputStream;public class TestObject extends Thread
{
private int i = 0;
public TestObject(int i)
{
this.i = i;
} public void run()
{
try
{
FileOutputStream out = new FileOutputStream("C:/test.txt",true);
Test.t.w(out,"start write---------" + i + "\r\n");
out.flush();
out.close();
Test.t.mA(i);
Thread.sleep(1000);
out = new FileOutputStream("C:/test.txt",true);
Test.t.w(out,"end write---------" + i + "\r\n"); }
catch(Exception e)
{
System.out.println("TestObject--------------" + e.getMessage());
}
} public static void main(String[] args)
{
int i = 0;
while(true)
//for(int j = 0;j < 100;j++)
{
new TestObject(i++).start();
}
}
}class Test
{
public static Test t = new Test();
public synchronized void w(FileOutputStream out,String str)
{
try
{
out.write(str.getBytes());
out.flush();
out.close();
}
catch(Exception e)
{
System.out.println("Test.w--------------" + e.getMessage());
}
} public void mA(int i)
{
try
{
Thread.sleep(5000);
FileOutputStream out = new FileOutputStream("C:/test.txt",true);
w(out,"Test.mA() write---------" + i + "\r\n");
out.flush();
out.close();
}
catch(Exception e)
{
System.out.println("Test.mA--------------" + e.getMessage());
}
}
}