Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java-曹志1407064122 #29

Open
fangcao1314 opened this issue Dec 25, 2015 · 7 comments
Open

java-曹志1407064122 #29

fangcao1314 opened this issue Dec 25, 2015 · 7 comments

Comments

@fangcao1314
Copy link

No description provided.

@fangcao1314
Copy link
Author

//因为这周通知考试只能有三天时间复习迟迟未上传
//真的是sorry,
//真的是sorry,
//真的是sorry,
//重要的事情说三遍!
//1、自动写文件

import java.io.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Filetext {

public static void main(String[] args) {
    File manyfile = new File("E://testforio");

    File file = null;
    try {
        if (!manyfile.exists()) {
            manyfile.mkdir();
        }
        for (int i = 0; i < 10; i++) {
            file = new File(manyfile, "helloworld" + i + ".txt");
            OutputStream os= new FileOutputStream(file);
            String s = "我真"+i;
            os.write(s.getBytes());

            if (!file.exists()) {
                file.createNewFile();
            }
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}}

@fangcao1314
Copy link
Author

//删羊1
ArrayList sheepList=new ArrayList();
sheepList.add(new Sheep("喜羊羊"));
sheepList.add(new Sheep("美羊羊"));
sheepList.add(new Sheep("慢羊羊"));
sheepList.add(new Sheep("沸羊羊"));
sheepList.add(new Sheep("懒羊羊"));
for(int j=1;j<4;j++)
sheepList.remove(2);
for (int i=0;i<sheepList.size();i++)
System.out.println(((Sheep)sheepList.get(i)).name);

}

@fangcao1314
Copy link
Author

//删羊2
ArrayList sheepList=new ArrayList();
sheepList.add(new Sheep("喜羊羊"));
sheepList.add(new Sheep("美羊羊"));
sheepList.add(new Sheep("慢羊羊"));
sheepList.add(new Sheep("沸羊羊"));
sheepList.add(new Sheep("懒羊羊"));

for( int j=0;j<3;j++)

sheepList.remove(sheepList.size()-1);
for (int i=0;i<sheepList.size();i++)//每次删除最后一只
System.out.println(((Sheep)sheepList.get(i)).name);
}

@fangcao1314
Copy link
Author

//删羊3
ArrayList sheepList=new ArrayList();
sheepList.add(new Sheep("喜羊羊"));
sheepList.add(new Sheep("美羊羊"));
sheepList.add(new Sheep("慢羊羊"));
sheepList.add(new Sheep("沸羊羊"));
sheepList.add(new Sheep("懒羊羊"));
for(int i=0;i<2;i++)
sheepList.remove(3);
sheepList.remove(2);

for (int i=0;i<sheepList.size();i++)
    System.out.println(((Sheep)sheepList.get(i)).name);

@fangcao1314
Copy link
Author

//线程—Thread

public class Thread_me extends Thread {
public Thread_me(String name){
super (name);

}
public Thread_me(){
    super();

}
 private int i;
 public void run()
 {
     for(;i<4;i++)
     {
         System.out.println(getName()+" "+i);
     }
 }
public static void main(String[] args) {
    for(int i=0;i<4;i++)
    {
        System.out.println(Thread.currentThread().getName()+" 我是主线程我运行第"+i+"次");

    }       

// new Thread_me().start();
// new Thread_me().start();
Thread_me h1=new Thread_me("我是一号");
Thread_me h2=new Thread_me("我是二号");
Thread_me h3=new Thread_me("我是三号");
h1.start();
h2.start();
h3.start();

}

}

@fangcao1314
Copy link
Author

//线程-Runnable

public class Runable_me implements Runnable{
public static void main(String[] args) {
for(int i=0;i<4;i++)
{
System.out.println(Thread.currentThread().getName()+""+i);
Runable_me h1=new Runable_me();
Runable_me h2=new Runable_me();
Runable_me h3=new Runable_me();
new Thread(h1 ,"新线程一").start();
new Thread(h2 ,"新线程二").start();
new Thread(h3 ,"新线程三").start();
}

}

@Override
public void run() {
    // TODO Auto-generated method stub
    for (int i =0;i<4;i++)
    System.out.println(Thread.currentThread().getName()+""+i);
}

}

@fangcao1314
Copy link
Author

在java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口;Thread类是在java.lang包中定义的。一个类只要继承了Thread类同时覆写了本类中的run()方法就可以实现多线程操作了,但是一个类只能继承一个父类,这是此方法的局限。
在实际开发中一个多线程的操作很少使用Thread类,而是通过Runnable接口完成。
但是在使用Runnable定义的子类中没有start()方法,只有Thread类中才有。此时观察Thread类,有一个构造方法:public Thread(Runnable targer)此构造方法接受Runnable的子类实例,也就是说可以通过Thread类来启动Runnable实现的多线程。
在程序开发中只要是多线程肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下好处:
避免点继承的局限,一个类可以继承多个接口。
适合于资源的共享

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant