- 、如何控制线程的执行顺序?(join()方法)
public class ThreadByOrder
{
public static void main(String[] args) throws InterruptedException
{
Thread t1 = new Thread(new MyThread1());
Thread t2 = new Thread(new MyThread2());
Thread t3 = new Thread(new MyThread3());
/*t1.start();
t2.start();
t3.start();*/
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
}
}
class MyThread1 implements Runnable
{
public void run()
{
System.out.println("I am thread 1");
}
}
class MyThread2 implements Runnable
{
public void run()
{
System.out.println("I am thread 2");
}
}
class MyThread3 implements Runnable
{
public void run()
{
System.out.println("I am thread 3");
}
}
参考:https://blog.csdn.net/u011519624/article/details/79039380
2、Java中wait()和sleep()的异同?
在等待的时候wait会释放锁,一般用于线程交互,sleep持有锁,用于暂停执行。
3、什么是原子操作?Java中原子操作是什么?
4、volatile和synchronized异同?
volatile在并发环境中确保可见性、顺序性、一致性。
5、多线程要调用start方法,不直接调用run方法?
调用start方法是创建线程,并执行run方法,直接调用run的时候不创建新线程不会调用线程代码。
6、多线程中遇到的问题和解决方法?
如:memory-interface、竞争、死锁、活锁、饥饿等。
7、什么是多线程上下文切换?