package Demo1;
//多线程会提高程序的与运行效率吗? 不会,一个人在多个桌子上做馒头,和在一个桌子上做馒头
//为什么会有多线程下载呢? 多线程下载是抢了服务器的带宽,一个人分配一定的带宽,多个人分配更多的带宽,所以更快。
public class TranditionalThread {
public static void main(String args[]){
//第一种写法
Thread thread =new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread1"+Thread.currentThread().getName());
//System.out.println(this.getName());
}
}
};
thread.start();
//第二种写法
/*Thread thread2= new Thread(new Runnable(){
@SuppressWarnings("static-access")
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread2"+Thread.currentThread().getName());
}
}
});
thread2.start();
*/
//第三种写法 执行的是Thread4,先去找子类对象中的run方法,如果子类中没有,在从父类中去找
/* new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread3"+Thread.currentThread().getName());
}
}
}){
public void run() {
while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread4"+Thread.currentThread().getName());
}
};
}.start();
*/
}
}
//多线程会提高程序的与运行效率吗? 不会,一个人在多个桌子上做馒头,和在一个桌子上做馒头
//为什么会有多线程下载呢? 多线程下载是抢了服务器的带宽,一个人分配一定的带宽,多个人分配更多的带宽,所以更快。
public class TranditionalThread {
public static void main(String args[]){
//第一种写法
Thread thread =new Thread(){
@Override
public void run() {
while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread1"+Thread.currentThread().getName());
//System.out.println(this.getName());
}
}
};
thread.start();
//第二种写法
/*Thread thread2= new Thread(new Runnable(){
@SuppressWarnings("static-access")
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread2"+Thread.currentThread().getName());
}
}
});
thread2.start();
*/
//第三种写法 执行的是Thread4,先去找子类对象中的run方法,如果子类中没有,在从父类中去找
/* new Thread(new Runnable(){
@Override
public void run() {
while(true){
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread3"+Thread.currentThread().getName());
}
}
}){
public void run() {
while(true){
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread4"+Thread.currentThread().getName());
}
};
}.start();
*/
}
}