1, multi-threaded (single case design mode)
2. Multithreading (Runtime Class)
- The runtime class is a singleton class
3. Multithreading (Timer)
Timer class: Timer
public class Demo5_Timer { /** * @param args * 计时器 * @throws InterruptedException */ public static void main(String[] args) throws InterruptedException { Timer t = new Timer(); t.schedule(new MyTimerTask(), new Date(114,9,15,10,54,20),3000); while(true) { System.out.println(new Date()); Thread.sleep(1000); } } } class MyTimerTask extends TimerTask { @Override public void run() { System.out.println("起床背英语单词"); } }
4. Multithreading (communication between two threads)
- 1. When to communicate
- When multiple threads are executing concurrently, the CPU is randomly switching threads by default
- If we want them to execute regularly, we can use communication, for example, each thread performs a print
- 2. How to Communicate
- Call Wait () if you want the thread to wait
- If you want to wake up the waiting thread, call notify ();
- Both methods must be executed in the synchronization code, and the synchronization lock object is used to invoke the
5, multi-threaded (three or more than three threads communication)
- Issues with multiple thread communication
- The Notify () method is a random wake-up of a thread
- The Notifyall () method is to wake all threads
- JDK5 Unable to wake a specified thread before
- If the communication between multiple threads, you need to use Notifyall () to notify all threads, use while to repeatedly judge the condition
6, Multi-threaded (JDK1.5 new feature mutex)
- 1. Synchronization
- Synchronizing using the Lock () and Unlock () Methods of the Reentrantlock class
- 2. Communication
- Use the Newcondition () method of the Reentrantlock class to get the condition object
- Use condition's await () method when you need to wait, wake up with the signal () method
- Different threads use different condition so that you can tell which thread to look for when waking up.
7. Multithreading (Overview and use of thread groups)
- A: Thread Group Overview
- Java uses threadgroup to represent thread groups, which can classify a batch of threads, and Java allows programs to control thread groups directly.
- By default, all threads belong to the main thread group.
- Public Final Threadgroup Getthreadgroup ()//Gets the group that he belongs to by thread object
- public final String getName ()//through Thread Group object Get the name of his group
- We can also set the thread to group
- 1,threadgroup (String name) to create a thread group object and assign it a name
- 2, creating a line Path Object
- 3,thread (Threadgroup?group, Runnable?target, String?name)
- 4, set the priority of the whole group or daemon
- B: Case demo
- thread group usage, default is main thread group
-
myrunnable mr = New Myrunnable (); thread T1 = new Thread (MR, "Zhang San"); Thread t2 = new Thread (MR, "John Doe");//Get thread Group//thread class inside Method: Public final Threadgroup Getthreadgroup () threadgroup TG1 = T1.getthrea DGroup (); Threadgroup TG2 = T2.getthreadgroup ();//method inside the thread group: public final String GetName () string name1 = Tg1.getname (); String name2 = Tg2.getname (); System.out.println (NAME1); System.out.println (name2);//Through the results we know: The thread by default belongs to the main thread group//through the following test, you should be able to see that, under the circumstances, all threads belong to the same group SYSTEM.OUT.PRINTLN (
Thread.CurrentThread (). Getthreadgroup (). GetName ());
- Set Thread GROUP by itself
// ThreadGroup(String name)ThreadGroup tg = new ThreadGroup("这是一个新的组");MyRunnable mr = new MyRunnable();// Thread(ThreadGroup group, Runnable target, String name)Thread t1 = new Thread(tg, mr, "张三");Thread t2 = new Thread(tg, mr, "李四");System.out.println(t1.getThreadGroup().getName());System.out.println(t2.getThreadGroup().getName());//通过组名称设置后台线程,表示该组的线程都是后台线程tg.setDaemon(true);
8. Multithreading (five states of threads)
- Look at the picture and talk
- New, ready, running, blocking, dead
9. Multithreading (Overview and use of thread pool)
10. Multithreading (How to implement multithreaded programs 3)
It was submitted by callable.
// 创建线程池对象ExecutorService pool = Executors.newFixedThreadPool(2);// 可以执行Runnable对象或者Callable对象代表的线程Future<Integer> f1 = pool.submit(new MyCallable(100));Future<Integer> f2 = pool.submit(new MyCallable(200));// V get()Integer i1 = f1.get();Integer i2 = f2.get();System.out.println(i1);System.out.println(i2);// 结束pool.shutdown();public class MyCallable implements Callable<Integer> { private int number; public MyCallable(int number) { this.number = number; } @Override public Integer call() throws Exception { int sum = 0; for (int x = 1; x <= number; x++) { sum += x; } return sum; }}
Ways to implement multithreaded programs 3 benefits and drawbacks
Benefits:
- Can have a return value
- can throw an exception
Disadvantages:
- The code is more complex, so it's generally not
11, design mode (simple Factory mode overview and use)
12. Design pattern (Overview and use of factory method mode)
13, GUI (How to create a window and display)
- Graphical user Interface (GUI).
Frame f = new Frame(“my window”);f.setLayout(new FlowLayout());//设置布局管理器f.setSize(500,400);//设置窗体大小f.setLocation(300,200);//设置窗体出现在屏幕的位置f.setIconImage(Toolkit.getDefaultToolkit().createImage("qq.png"));f.setVisible(true);
14. GUI (layout manager)
- FlowLayout (Streaming layout manager)
- Ordered from left to right.
- Panel default layout manager.
- BorderLayout (Boundary layout manager)
- East, south, west, north, middle
- The default layout manager for frame.
- GridLayout (Grid layout manager)
- CardLayout (Card layout manager)
- GridBagLayout (Grid package layout manager)
15. GUI (Form monitoring)
Frame f = new Frame("我的窗体");//事件源是窗体,把监听器注册到事件源上//事件对象传递给监听器f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { //退出虚拟机,关闭窗口 System.exit(0); }});
16, GUI (mouse monitoring) 17, GUI (keyboard monitoring and keyboard events) 18, GUI (action monitoring)19. Design mode (adapter design mode)
- A. What is an adapter
- When using the listener, you need to define a class event listener interface.
- Usually there are multiple methods in the interface, but it's not necessarily all in the program, but it has to be rewritten, which is tedious.
- The adapter simplifies these operations by simply inheriting the adapter when we define the listener, and then overriding the required methods.
- B. Adapter principle
- The adapter is a class that implements the listener interface, and all the abstract methods are rewritten, but the methods are all empty.
- The adapter class needs to be defined as abstract because it is meaningless to call an empty method because the class object is created.
- The goal is to simplify the programmer's operation, to define the listener to inherit the adapter, only to rewrite the required methods.
20, GUI (need to know)
- Event handling
- Event: An action for the user
- Event Source: The component being manipulated
- Listener: An object of a custom class, implements the listener interface, contains the event handler, adds the listener to the event source, and when the event occurs, the virtual machine automatically invokes the event-handling method in the listener.
Java EE Fundamentals (25)/multithreaded, GUI