多线程超级汇总

1. what different between thread and process.
since a process can consist of multiple threads, a thread could be considered a ‘lightweight’ process. Thus, the essential difference between a thread and a process is the work that each one is used to accomplish. Threads are used for small tasks, whereas processes are used for more ‘heavyweight’ tasks – basically the execution of applications.

Another difference between a thread and a process is that threads within the same process share the same address space, whereas different processes do not. This allows threads to read from and write to the same data structures and variables, and also facilitates communication between threads. Communication between processes – also known as IPC, or inter-process communication – is quite difficult and resource-intensive.
2. how many ways implement multithread in java

1) inherit the thread class
public class classA() extends Thread {            // 继承Thread类
 
  public void run () {                            // 覆盖run()方法
    system.out.println("multi-thread");
  }
 
  pubic static void main(String args[]) {
    classA clsA = new classA();
    clsA.start();                                 // 启动线程
  }
 
}


2)implement the interface of runnable class 
public class classB() implements Runable {         // 实现Runnable接口
 
  public void run() {                             // 覆盖run()方法
   system.out.println("multi-thread");
  }
 
  public void main (string args[]) {
    classB clsB = new classB();
    Thread t1 = new Thread(clsB);
    t1.start();                                 // 启动线程
  }
}
3.how many ways to implement synchronized 
1)synchronized method
public void synchronized add(){                // 同步方法
  sleep(100);
  sytem.out.println("sleep");
}
2)synchronized blocks.
public void main(string args[]){
  private Well well;
 
  public class Well() {
    system.out.println("Well");
  }
 
  sychronized(well){                             // 同步代码块
    String a = "";
    system.out.println(a);
  }
 
  public viod methodB(){
   system.out.println("methodB");
  }
}
4.今天 看到一道百度笔试题 
以下多线程对int型变量x的操作,哪几个需要进行同步: 
A. x=y;         B. x++;         C. ++x;            D. x=1; 
  
最初有人说选B 因为操作了2个寄存器。答案:ABC 
  
后面干脆将代码汇编了。 
得到 
x = y;
00411A25 mov eax,dword ptr [y] 
00411A28 mov dword ptr [x],eax 

x++;
00411A2B mov eax,dword ptr [x] 
00411A2E add eax,1 
00411A31 mov dword ptr [x],eax 

++x;
00411A34 mov eax,dword ptr [x] 
00411A37 add eax,1 
00411A3A mov dword ptr [x],eax 

x = 1;
00411A3D mov dword ptr [x],1
编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。

明显要求就是3个线程轮流执行一次,然后循环10次,关键就是如何控制。

public class ThunderTest {    
    private boolean isThreadA = true;
    private boolean isThreadB = false;
    private boolean isThreadC = false;
    
    private Object Lock = new Object();//互斥器
    
    private static int PRINTTIMES = 10;
    
    public static void main(String[] args) {
        ThunderTest tt = new ThunderTest();
        tt.PRINTTIMES = 10;
        new Thread(tt.new ThreadA()).start();
        new Thread(tt.new ThreadB()).start();
        new Thread(tt.new ThreadC()).start();
    }
    
    public class ThreadA implements Runnable{
 
        public void run() {
            int count = 0;
            while(count<PRINTTIMES)
            {
                synchronized (Lock) {
                    if(isThreadA)
                    {
                        System.out.print("A");//打印A
                        count++;
                        isThreadA = false;
                        isThreadB = true;//B可以打印了
                        isThreadC = false;
                        Lock.notifyAll();//唤醒所有线程,为true的可以打印
                        
                    }
                    else
                    {
                        try {
                            Lock.wait();//false,释放锁,wait
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    public class ThreadB implements Runnable{
 
        public void run() {
            int count = 0;
            while(count < PRINTTIMES)
            {
                synchronized (Lock) {
                    if(isThreadB)
                    {
                        System.out.print("B");
                        count++;
                        isThreadA = false;
                        isThreadB = false;
                        isThreadC = true;
                        Lock.notifyAll();
                    }
                    else{
                        try {
                            Lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
    
    public class ThreadC implements Runnable{
        
        public void run() {
            int count = 0;
            while(count<PRINTTIMES)
            {
                synchronized (Lock) {
                    if(isThreadC)
                    {
                        System.out.print("C");
                        count++;
                        isThreadA = true;
                        isThreadB = false;
                        isThreadC = false;
                        Lock.notifyAll();
                    }
                    else{
                        try {
                            Lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        
    }
}

 

posted on 2014-06-23 07:01  brave_bo  阅读(242)  评论(0)    收藏  举报

导航