//在学习多线程的过程中要知道每次的线程结果都可能不相同(和CPU的分配有关),要多运行几次找到规律
//关于多个线程操作一个对象和多个线程操作多个对象
–多线程操作一个对象就可以分享一个资源(多线程单个对象容易引发并发问题)
多线程操作多个对象()
1.线程的定义(每个类后面可以加引号,个人习惯不同)
继承Thread方法实现多线程
实现Runnable接口实现多线程
两种方法的联系
两种方法的区别
start是什么时候调用run()方法的
/**
* Causes this thread to begin execution; the Java Virtual Machine
* calls the <code>run</code> method of this thread.
* <p>
* The result is that two threads are running concurrently: the
* current thread (which returns from the call to the
* <code>start</code> method) and the other thread (which executes its
* <code>run</code> method).
* <p>
* It is never legal to start a thread more than once.
* In particular, a thread may not be restarted once it has completed
* execution.
*
* @exception IllegalThreadStateException if the thread was already
* started.
* @see #run()
* @see #stop()
*/
public synchronized void start()
这个问题有点意思,因为在Thread的start中并没有显式调用run方法,不过研究上面的start这个方法的注释,注意第一行就可以发现jvm会调用thread的run()方法
2.多线程的操作
Thread.currentThread.getname(); 得到当前线程的名字
xx.isAlive();判断线程是否存活
--代码执行之后可能为TRUE也可能为FALSE,如果到该点分线程在main线程之前完成则为FALSE,线程已经完毕
线程的强制运行,执行join方法之后只能等join中的线程运行完毕才能运行其他线程。
try
{
t1.join();
}
catch (InterruptedException e)
{
}
线程的休眠
线程的终止
线程Thread_0本应该休眠十秒在执行却在两秒后被主线程interrupt中断。休眠一旦终止就执行catch中的代码。
后台线程
线程的优先级(priority)
线程的礼让
示例
继承thread接口
implement runnable实例Runnable接口
死锁与同步
使用同步解决问题
死锁
线程操作实例
package cn.edu.wic.DXCsisuo;
class Info{
private String name = "zjl";
private String context = "加油,努力";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getContext()
{
return context;
}
public void setContext(String context)
{
this.context = context;
}
}
class producer implements Runnable{
private Info info = null;
public producer(Info info){
this.info=info;
}
public void run(){
boolean flag = false;
for(int i=0;i<10;i++){
if(flag){
this.info.setName("zjl");
try
{
Thread.sleep(90);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.info.setContext("加油,努力");
flag = false;
}else{
this.info.setName("我是zjl");
try
{
Thread.sleep(90);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.info.setContext("我一定会成功的");
flag = true;
}
}
}
}
class Consumer implements Runnable{
private Info info = null;
public Consumer(Info info){
this.info = info;
}
public void run(){
for(int i=0;i<10;i++){
try
{
Thread.sleep(100);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.info.getName()+"----->"+this.info.getContext());
}
}
}
public class ScXfTest
{
/**
*
*/
public static void main(String[] args)
{
Info i = new Info();
producer pr = new producer(i);
Consumer con = new Consumer(i);
new Thread(pr).start();
new Thread(con).start();
}
}
package cn.edu.wic.DXCsisuoTest;
class Info{
private String name = "zjl";
private String context = "加油,努力";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getContext()
{
return context;
}
public void setContext(String context)
{
this.context = context;
}
public synchronized void set(String name,String context){
this.setName(name);
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.setContext(context);
}
public synchronized void get(){
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.getName()+"-->"+this.getContext());
}
}
class producer implements Runnable{
private Info info = null;
public producer(Info info){
this.info= info;
}
public void run(){
boolean flag = false;
for(int i=0;i<20;i++){
if(flag){
this.info.set("zjl", "加油努力");
flag= false;
}else {
this.info.set("zjl", "我一定会成功的");
flag=true;
}
}
}
}
class Consumer implements Runnable{
private Info info = null;
public Consumer(Info info){
this.info=info;
}
public void run(){
for(int i=0;i<20;i++){
try
{
Thread.sleep(130);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.info.get();
}
}
}
public class ScxfTestDemo1
{
public static void main(String[] args)
{
Info i = new Info();
producer producer = new producer(i);
Consumer consumer = new Consumer(i);
new Thread(producer).start();
new Thread(consumer).start();
}
}
package cn.edu.wic.DXCsisuoTest;
class Info3 {
private String name = "zjl";
private String context = "加油,努力";
boolean flag = false;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getContext()
{
return context;
}
public void setContext(String context)
{
this.context = context;
}
public synchronized void set(String name, String context){ //同步方法
if(!flag){
try
{
super.wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
this.setName(name);
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
this.setContext(context);
flag = false;
super.notify();
}
public synchronized void get(){
if(flag){
try
{
super.wait(); //由于wait方法是Object类对线程的一个方法,而且是final修饰的方法,所以只能是调用父类的方法,使用super
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
try
{
Thread.sleep(30);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
System.out.println(this.getName()+"---"+this.getContext());
flag = true;
super.notify();
}
}
class Producer3 implements Runnable{
private Info3 info = null;
public Producer3 (Info3 info){
this.info=info;
}
public void run(){
boolean flag = false;
for(int i=0;i<20;i++){
if (flag)
{
this.info.set("zjl", "加油,努力");
flag = false;
}
else
{
this.info.set("zjl", "我一定会成功");
flag=true;
}
}
}
}
class Consumer3 implements Runnable{
private Info3 info =null;
public Consumer3(Info3 info){
this.info = info;
}
public void run(){
for(int i=0;i<20;i++){
this.info.get();
}
}
}
public class ScxfTestDemo3
{
/**
* @param args
*/
public static void main(String[] args)
{
Info3 info = new Info3();
Producer3 producer3 = new Producer3(info);
Consumer3 consumer3 = new Consumer3(info);
new Thread(producer3).start();
new Thread(consumer3).start();
}
}