黑马程序员----多线程
2014-05-31 19:22
411 查看
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
多线程有两种方式:1,继承Thread 2实现一个接口Runnable。
第一种方式步骤:
1,定义类继承Thread类;
2,目的是复写run方法,将要让线程运行的代码都存储到run方法中;
3,通过创建Thread类的子类对象,创建线程
4,调用线程的start方法,开启线程,并执行run方法。
/*class ThreadTest
{
public static void main(String[] args)
{
PrintThread p1 = new PrintThread("2222");
PrintThread p2 = new PrintThread("444");
p1.start();
p2.start();
}
}
class PrintThread extends Thread
{
PrintThread(String name)
{
super(name);
}
public void run()
{
for (int x=0;x<100 ;x++ )
{
System.out.println(Thread.currentThread().getName()+" x="+x);
}
}
}
*/
class PrintThread implements Runnable
{
Object obj = new Object();
private int tick=800;
public void run()
{
while(true)
{
synchronized(this){
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+" tick="+tick--);
}
else
break;
}}
}
}
class ThreadTest
{
public static void main(String[] args)
{
PrintThread p = new PrintThread();
Thread p1 = new Thread(p);
Thread p2 = new Thread(p);
Thread p3 = new Thread(p);
Thread p4 = new Thread(p);
p1.start();
p2.start();
p3.start();
p4.start();
}
}
第二种方式步骤:
1,定义类实现Runnable接口。
2,覆盖接口中的run方法(用于封装线程要运行的代码)。
3,通过Thread类创建线程对象;
4,将实现了Runnable接口的子类对象作为实际参数传递给Thread类中的构造函数。
5,调用Thread对象的start方法。开启线程,并运行Runnable接口子类中的run方法。
class InputOutputDemo
{
public static void main(String[] args)
{
res r = new res();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
class res
{
String name;
String sex;
boolean flog = false;
}
class Input implements Runnable
{
private res r;
Input(res r)
{
this.r=r;
}
public void run()
{
int x = 0;
while(true)
{
synchronized(r)
{
if (r.flog)
{
try
{
r.wait();
}
catch (Exception e)
{
}
}
if (x==1)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女";
}
r.flog = true;
x=(x+1)%2;
r.notify();
}
}
}
}
class Output implements Runnable
{
private res r;
Output(res r)
{
this.r= r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if (!r.flog)
{
try
{
r.wait();
}
catch (Exception e)
{
}
}
System.out.println(r.name+"========"+r.sex);
r.flog = false;
r.notify();
}
}
}
}
多线程安全问题的原因:
一个线程在执行多条语句时,并运算同一个数据时,在执行过程中,其他线程参与进来,并操作了这个数据。导致到了错误数据的产生。
死锁练习
public class TestDeadLock implements Runnable
{
public int flag = 3;
public static Object o1 = new Object();
public static Object o2 = new Object();
public void run()
{
System.out.println("flag:"+flag);
if (flag == 1)
{
synchronized (o1)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (o2)
{
System.out.println("111");
}
}
} else if (flag == 0)
{
synchronized (o2)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (o1)
{
System.out.println("000");
}
}
}
}
public static void main(String[] args)
{
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
多线程有两种方式:1,继承Thread 2实现一个接口Runnable。
第一种方式步骤:
1,定义类继承Thread类;
2,目的是复写run方法,将要让线程运行的代码都存储到run方法中;
3,通过创建Thread类的子类对象,创建线程
4,调用线程的start方法,开启线程,并执行run方法。
/*class ThreadTest
{
public static void main(String[] args)
{
PrintThread p1 = new PrintThread("2222");
PrintThread p2 = new PrintThread("444");
p1.start();
p2.start();
}
}
class PrintThread extends Thread
{
PrintThread(String name)
{
super(name);
}
public void run()
{
for (int x=0;x<100 ;x++ )
{
System.out.println(Thread.currentThread().getName()+" x="+x);
}
}
}
*/
class PrintThread implements Runnable
{
Object obj = new Object();
private int tick=800;
public void run()
{
while(true)
{
synchronized(this){
if(tick>0)
{
try{Thread.sleep(10);}catch(Exception e){}
System.out.println(Thread.currentThread().getName()+" tick="+tick--);
}
else
break;
}}
}
}
class ThreadTest
{
public static void main(String[] args)
{
PrintThread p = new PrintThread();
Thread p1 = new Thread(p);
Thread p2 = new Thread(p);
Thread p3 = new Thread(p);
Thread p4 = new Thread(p);
p1.start();
p2.start();
p3.start();
p4.start();
}
}
第二种方式步骤:
1,定义类实现Runnable接口。
2,覆盖接口中的run方法(用于封装线程要运行的代码)。
3,通过Thread类创建线程对象;
4,将实现了Runnable接口的子类对象作为实际参数传递给Thread类中的构造函数。
5,调用Thread对象的start方法。开启线程,并运行Runnable接口子类中的run方法。
class InputOutputDemo
{
public static void main(String[] args)
{
res r = new res();
new Thread(new Input(r)).start();
new Thread(new Output(r)).start();
}
}
class res
{
String name;
String sex;
boolean flog = false;
}
class Input implements Runnable
{
private res r;
Input(res r)
{
this.r=r;
}
public void run()
{
int x = 0;
while(true)
{
synchronized(r)
{
if (r.flog)
{
try
{
r.wait();
}
catch (Exception e)
{
}
}
if (x==1)
{
r.name="mike";
r.sex="man";
}
else
{
r.name="丽丽";
r.sex="女";
}
r.flog = true;
x=(x+1)%2;
r.notify();
}
}
}
}
class Output implements Runnable
{
private res r;
Output(res r)
{
this.r= r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if (!r.flog)
{
try
{
r.wait();
}
catch (Exception e)
{
}
}
System.out.println(r.name+"========"+r.sex);
r.flog = false;
r.notify();
}
}
}
}
多线程安全问题的原因:
一个线程在执行多条语句时,并运算同一个数据时,在执行过程中,其他线程参与进来,并操作了这个数据。导致到了错误数据的产生。
死锁练习
public class TestDeadLock implements Runnable
{
public int flag = 3;
public static Object o1 = new Object();
public static Object o2 = new Object();
public void run()
{
System.out.println("flag:"+flag);
if (flag == 1)
{
synchronized (o1)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (o2)
{
System.out.println("111");
}
}
} else if (flag == 0)
{
synchronized (o2)
{
try
{
Thread.sleep(10);
} catch (InterruptedException e)
{
e.printStackTrace();
}
synchronized (o1)
{
System.out.println("000");
}
}
}
}
public static void main(String[] args)
{
TestDeadLock td1 = new TestDeadLock();
TestDeadLock td2 = new TestDeadLock();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Unity开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------
相关文章推荐
- 黑马程序员 java多线程笔记(二)
- 黑马程序员——Java之多线程(2)
- 黑马程序员D8 多线程
- 黑马程序员——Java基础——多线程
- 黑马程序员——多线程(一)
- 黑马程序员—【Java基础篇】之多线程
- 黑马程序员——java编程那些事儿____多线程(一)
- 黑马程序员---java基础---多线程
- 黑马程序员java笔记之二-----多线程
- 黑马程序员 —— Java多线程1 (第十一天)
- 黑马程序员--多线程
- 黑马程序员-------多线程(一)
- 黑马程序员————多线程
- 黑马程序员_多线程(线程安全和通信)
- 黑马程序员—多线程的学习(1)
- “黑马程序员”多线程
- 黑马程序员--深入探究多线程
- 黑马程序员--多线程
- 黑马程序员 (1) 对JAVA多线程编程进行学习。
- 奋斗黑马程序员----Java之多线程讲解(二)