2个回答
展开全部
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ThreadPerformed extends JFrame implements ActionListener{
JButton jbOk=new JButton("确定");
double test;
public ThreadPerformed() {
this.setTitle("多线程优化界面的显示");
this.setBounds(200,300,350,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbOk.setMargin(new Insets(0,0,0,0));
this.add(jbOk,BorderLayout.NORTH);
jbOk.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
MyTread tt=new MyTread();
tt.start();
/* 没有利用多线程点击完确定按钮后,界面由于进行运算而不能响应,
for (long i=0;i<1000000000;i++){
test=test+Math.random()*Math.PI;
}
*/
}
public static void main (String[] args) {
new ThreadPerformed();
}
}
//开辟一个单独的线程,负责复杂计算,点击完确定按钮后,界面立刻就响应,计算在后台进行
class MyTread extends Thread {
double test;
public void run(){
for(long i=0;i<1000000000;i++) {
test=test+Math.random()*Math.PI;
}
}
}
import java.awt.event.*;
import javax.swing.*;
public class ThreadPerformed extends JFrame implements ActionListener{
JButton jbOk=new JButton("确定");
double test;
public ThreadPerformed() {
this.setTitle("多线程优化界面的显示");
this.setBounds(200,300,350,200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jbOk.setMargin(new Insets(0,0,0,0));
this.add(jbOk,BorderLayout.NORTH);
jbOk.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
MyTread tt=new MyTread();
tt.start();
/* 没有利用多线程点击完确定按钮后,界面由于进行运算而不能响应,
for (long i=0;i<1000000000;i++){
test=test+Math.random()*Math.PI;
}
*/
}
public static void main (String[] args) {
new ThreadPerformed();
}
}
//开辟一个单独的线程,负责复杂计算,点击完确定按钮后,界面立刻就响应,计算在后台进行
class MyTread extends Thread {
double test;
public void run(){
for(long i=0;i<1000000000;i++) {
test=test+Math.random()*Math.PI;
}
}
}
展开全部
public class TestRunnable implements Runnable {
private int i ;
public void run() {
for( ; i < 30; i++){
System.out.println(Thread.currentThread().getName()+ " " + i + "ok");
}
}
public static void main(String[] args) {
int i = 0;
for(; i < 30; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
if(i == 10){
TestRunnable tr = new TestRunnable();
new Thread(tr, "子线程0").start();
new Thread(tr, "子线程1").start();
}
}
}
}
private int i ;
public void run() {
for( ; i < 30; i++){
System.out.println(Thread.currentThread().getName()+ " " + i + "ok");
}
}
public static void main(String[] args) {
int i = 0;
for(; i < 30; i++){
System.out.println(Thread.currentThread().getName() + " " + i);
if(i == 10){
TestRunnable tr = new TestRunnable();
new Thread(tr, "子线程0").start();
new Thread(tr, "子线程1").start();
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询