Scenario import
In the process of program execution, if a for loop is encountered, generally, the program will not start to execute the next instruction until the loop is completed; While we usually use computers, we can brush microblogs while listening to music, which is realized by multithreading.
Interpretation of professional terms
Thread is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operation unit in the process. A thread refers to a single sequential control flow in a process. Multiple threads can be concurrent in a process, and each thread executes different tasks in parallel (the above explanation comes from Baidu entry).
Example analysis
General method of drawing small ball:
public class Normal_Way extends JFrame{ public static void main(String[] agrs) { new Normal_Way(); } public Normal_Way() { // TODO Auto-generated constructor stub setTitle("Ordinary circle"); setSize(800,600); setDefaultCloseOperation(3); setLocationRelativeTo(null); setVisible(true); Graphics g=getGraphics(); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { int x=e.getX(); int y=e.getY(); Oval oval=new Oval(g,x,y,Color.black); } }); } }
public class Oval { Graphics g; int x; int y; Color color; public Oval(Graphics g,int x,int y,Color color) { // TODO Auto-generated constructor stub this.g=g; this.x=x; this.y=y; this.color=color; for(int i=0;i<50;i++) { try { //Increase the time interval for each cycle Thread.sleep(40); }catch (InterruptedException ex) { ex.printStackTrace(); } g.drawOval(x+5*i, y+5*i, 40, 40);//Draw a circle and change the position of the circle } } }
The running results of the program are as follows:
Although the program has run three times, each drawing starts after the last drawing. Is there any way to start the second drawing before the first drawing is completed?
Certainly. Use threads~
Method of implementing thread
There are two ways to implement threads: one has a return value and the other has no return value. Here are two ways to implement threads without a return value.
1 inherit the Thread class and override the run () method
First_Try.java
public class First_Try extends JFrame{ public static void main(String[] args) { First_Try ft1=new First_Try(); } public void paint(Graphics g) { super.paint(g); } public First_Try() { setTitle("Ball one"); setSize(800,600); setDefaultCloseOperation(3); setLocationRelativeTo(null); setVisible(true); Graphics g=this.getGraphics(); addMouseListener (new MouseAdapter() { public void mousePressed(MouseEvent e){ int x=e.getX (); int y=e.getY (); Color color=Color.black; RunBall ballRun1 = new RunBall (g,x,y,color); ballRun1.start ();// Start thread } }); } }
RunBall.java
public class RunBall extends Thread{ Graphics g; int x; int y; Color color; //Construction method public RunBall(Graphics g,int x,int y,Color color) { this.g=g; this.x=x; this.y=y; this.color=color; } //Override the run() method public void run() { for(int i=0;i<50;i++) { try { Thread.sleep(40); }catch (InterruptedException ex) { ex.printStackTrace(); } g.setColor(color); g.drawOval(x+5*i, y+5*i, 50, 50); } } }
The running results of the program are as follows
Just looking at the picture may be no different from the previous program, but this time you can draw the second graph before the first graph is completed, which is equivalent to you can listen to music while brushing your microblog with your mobile phone.
What is the effect of creating more threads?
Here, it is convenient to observe the running process of the program by changing the color of the brush.
We're at first_ Add the following code to the try class:
color=Color.red; RunBall ballRun2 = new RunBall (g,x,y,color); ballRun2.start ();// Start thread color=Color.blue; RunBall ballRun3 = new RunBall (g,x,y,color); ballRun3.start ();//Start thread
The running effect of the program is as follows:
2. Implement the Runable interface and rewrite the run method
Second_Try.java
public class Second_Try extends JFrame{ public static void main(String[] args) { new Second_Try(); } public Second_Try() { // TODO Auto-generated constructor stub setTitle("Ball two"); setSize(800,600); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); this.setVisible(true); Graphics g=this.getGraphics(); addMouseListener (new MouseAdapter() { public void mouseClicked(MouseEvent e) { int x=e.getX(); int y=e.getY(); // g.setColor(Color.blue); RunBall2 ball1=new RunBall2(g,x,y,Color.blue); new Thread(ball1).start();; // g.setColor(Color.red); RunBall2 ball2=new RunBall2(g,x,y,Color.red); new Thread(ball2).start(); // g.setColor(Color.yellow); RunBall2 ball3=new RunBall2(g,x,y,Color.yellow); new Thread(ball3).start(); } }); } public void paint(Graphics g) { super.paint(g); } }
RunBall2.java
public class RunBall2 implements Runnable{ Graphics g; int x; int y; Color color; public RunBall2(Graphics g,int x,int y,Color color) { // TODO Auto-generated constructor stub this.g=g; this.x=x; this.y=y; this.color=color; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<50;i++) { try { Thread.sleep(40); }catch (InterruptedException ex) { ex.printStackTrace(); } g.setColor(color); g.drawOval(x+5*i, y+5*i, 40, 40); } } }
The program runs as follows:
BUG resolution
In the process of implementing threads, you may encounter the problem of color coverage.
Can the following procedures achieve the above effect
public class Second_Try extends JFrame{ public static void main(String[] args) { new Second_Try(); } public Second_Try() { // TODO Auto-generated constructor stub setTitle("Ball two"); setSize(800,600); this.setDefaultCloseOperation(3); this.setLocationRelativeTo(null); this.setVisible(true); Graphics g=this.getGraphics(); addMouseListener (new MouseAdapter() { public void mouseClicked(MouseEvent e) { int x=e.getX(); int y=e.getY(); g.setColor(Color.blue);//newly added RunBall2 ball1=new RunBall2(g,x,y,Color.blue); new Thread(ball1).start();; g.setColor(Color.red);//newly added RunBall2 ball2=new RunBall2(g,x,y,Color.red); new Thread(ball2).start(); g.setColor(Color.yellow);//newly added RunBall2 ball3=new RunBall2(g,x,y,Color.yellow); new Thread(ball3).start(); } }); } public void paint(Graphics g) { super.paint(g); } }
public class RunBall2 implements Runnable{ Graphics g; int x; int y; Color color; public RunBall2(Graphics g,int x,int y,Color color) { // TODO Auto-generated constructor stub this.g=g; this.x=x; this.y=y; this.color=color; } @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<50;i++) { try { Thread.sleep(40); }catch (InterruptedException ex) { ex.printStackTrace(); } //g.setColor(color); g.drawOval(x+5*i, y+5*i, 40, 40); } } }
The program still works normally, but there is no effect of circle color alternation because of second_ The same brush is always used in the try class, and the last color of the brush is set to yellow, covering the other two colors, so there are only yellow circles.
The running results of the program are as follows:
expand
By slightly changing the following rewritten run() method, you can draw different beautiful and interesting graphics
Meter character
Romantic fireworks
Leave this to the readers to think for themselves~