当前位置: 首页 > news >正文

做网站公司赚钱百度海外广告运营

做网站公司赚钱,百度海外广告运营,淘宝运营培训班多少钱,个人网站趋向如果要使用Swing用J​​ava编写桌面或Java Web Start程序#xff0c;您可能会觉得需要通过创建自己的线程在后台运行某些东西。 没有什么可以阻止您在Swing中使用标准的多线程技术#xff0c;并且需要遵循通常的注意事项。 如果您有多个线程访问相同的变量#xff0c;则需要… 如果要使用Swing用J​​ava编写桌面或Java Web Start程序您可能会觉得需要通过创建自己的线程在后台运行某些东西。 没有什么可以阻止您在Swing中使用标准的多线程技术并且需要遵循通常的注意事项。 如果您有多个线程访问相同的变量则需要使用同步方法或代码块或诸如AtomicInteger或ArrayBlockingQueue之类的线程安全类。 但是对于那些粗心的人来说是一个陷阱。 与大多数用户界面API一样您无法从自己创建的线程更新用户界面。 好吧正如每个Java本科生都知道的那样您通常可以 但是您不应该。 如果这样做有时您的程序会运行而其他时候则无法。 您可以通过使用专门的SwingWorker类来解决此问题。 在本文中我将向您展示即使您正在使用Thread类如何使程序正常运行然后我们将继续研究SwingWorker解决方案。 为了演示我创建了一个Swing程序。 如您所见它由两个标签和一个开始按钮组成。 此刻单击开始按钮将调用一个不执行任何操作的处理程序方法。 这是Java代码 import java.awt.Font; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import java.util.concurrent.ExecutionException;import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.SwingUtilities; import javax.swing.SwingWorker;public class MainFrame extends JFrame {private JLabel countLabel1 new JLabel(0);private JLabel statusLabel new JLabel(Task not completed.);private JButton startButton new JButton(Start);public MainFrame(String title) {super(title);setLayout(new GridBagLayout());countLabel1.setFont(new Font(serif, Font.BOLD, 28));GridBagConstraints gc new GridBagConstraints();gc.fill GridBagConstraints.NONE;gc.gridx 0;gc.gridy 0;gc.weightx 1;gc.weighty 1;add(countLabel1, gc);gc.gridx 0;gc.gridy 1;gc.weightx 1;gc.weighty 1;add(statusLabel, gc);gc.gridx 0;gc.gridy 2;gc.weightx 1;gc.weighty 1;add(startButton, gc);startButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent arg0) {start();}});setSize(200, 400);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);}private void start() {}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {Overridepublic void run() {new MainFrame(SwingWorker Demo);}});} } 我们将添加一些代码到start方法中以响应单击开始按钮而调用。 首先让我们尝试一个普通线程。 private void start() {Thread worker new Thread() {public void run() {// Simulate doing something useful.for(int i0; i10; i) {// Bad practicecountLabel1.setText(Integer.toString(i));try {Thread.sleep(1000);} catch (InterruptedException e) {}}// Bad practicestatusLabel.setText(Completed.);}};worker.start();} 实际上此代码似乎有效至少对我而言。 该程序最终看起来像这样 但是不建议您这样做。 我们正在从自己的线程中更新GUI在某些情况下这肯定会引发异常。 如果要从另一个线程更新GUI则应使用SwingUtilities安排更新代码在事件分发线程上运行。 以下代码很好但像恶魔本人一样丑陋。 private void start() {Thread worker new Thread() {public void run() {// Simulate doing something useful.for(int i0; i10; i) {final int count i;SwingUtilities.invokeLater(new Runnable() {public void run() {countLabel1.setText(Integer.toString(count));}});try {Thread.sleep(1000);} catch (InterruptedException e) {}}SwingUtilities.invokeLater(new Runnable() {public void run() {statusLabel.setText(Completed.);}});}};worker.start();} 当然必须做些什么使我们的代码更优雅 SwingWorker类 SwingWorker是使用Thread class 专门为Swing设计的替代方法。 这是一个抽象类它带有两个模板参数这使它看起来非常凶猛并使大多数人不愿使用它。 但是实际上它并不像看起来那样复杂。 让我们看一些仅运行后台线程的代码。 对于第一个示例我们将不使用任何一个模板参数因此将它们都设置为Void 这是Java的等效于原始void类型的类带有小写的“ v”。 运行后台任务 通过实现doInBackground方法并调用execute来运行代码我们可以在后台运行任务。 SwingWorkerVoid, Void worker new SwingWorkerVoid, Void() {Overrideprotected Void doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);System.out.println(Running i);}return null;}};worker.execute(); 请注意 SwingWorker是一站式服务因此如果我们想再次运行代码则需要创建另一个SwingWorker 您无法重新启动同一台。 很简单嘿 但是如果我们想在运行代码后以某种状态更新GUI该怎么办 您无法从doInBackground更新GUI因为它不在主事件分配线程中运行。 但是有一个解决方案。 我们需要利用第一个模板参数。 线程完成后更新GUI 我们可以通过从doInBackground()返回一个值然后doInBackground() done()来更新GUI从而可以安全地更新GUI。 我们使用get()方法检索从doInBackground()返回的值 因此第一个模板参数确定doInBackground()和get()的返回类型。 SwingWorkerBoolean, Void worker new SwingWorkerBoolean, Void() {Overrideprotected Boolean doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);System.out.println(Running i);}// Here we can return some object of whatever type// we specified for the first template parameter.// (in this case were auto-boxing true).return true;}// Can safely update the GUI from this method.protected void done() {boolean status;try {// Retrieve the return value of doInBackground.status get();statusLabel.setText(Completed with status: status);} catch (InterruptedException e) {// This is thrown if the threads interrupted.} catch (ExecutionException e) {// This is thrown if we throw an exception// from doInBackground.}}};worker.execute(); 如果我们要在进行过程中更新GUI怎么办 这就是第二个模板参数的用途。 从正在运行的线程更新GUI 要从正在运行的线程更新GUI我们使用第二个模板参数。 我们调用publish()方法来“发布”我们要用来更新用户界面的值可以是第二个模板参数指定的任何类型。 然后我们重写process()方法该方法接收我们发布的值。 实际上process()接收已发布值的列表因为在实际调用process()之前可能会发布多个值。 在此示例中我们只是将最新值发布到用户界面。 SwingWorkerBoolean, Integer worker new SwingWorkerBoolean, Integer() {Overrideprotected Boolean doInBackground() throws Exception {// Simulate doing something useful.for (int i 0; i 10; i) {Thread.sleep(1000);// The type we pass to publish() is determined// by the second template parameter.publish(i);}// Here we can return some object of whatever type// we specified for the first template parameter.// (in this case were auto-boxing true).return true;}// Can safely update the GUI from this method.protected void done() {boolean status;try {// Retrieve the return value of doInBackground.status get();statusLabel.setText(Completed with status: status);} catch (InterruptedException e) {// This is thrown if the threads interrupted.} catch (ExecutionException e) {// This is thrown if we throw an exception// from doInBackground.}}Override// Can safely update the GUI from this method.protected void process(ListInteger chunks) {// Here we receive the values that we publish().// They may come grouped in chunks.int mostRecentValue chunks.get(chunks.size()-1);countLabel1.setText(Integer.toString(mostRecentValue));}};worker.execute(); 我希望您喜欢这个对高度有用的SwingWorker类的介绍。 您可以在我的网站Cave of Programming中找到更多教程包括有关多线程的完整免费视频课程以及有关SwingAndroid和Servlets 的课程 。 参考来自Java出现日历博客的JCG合作伙伴 John Purcell的SwingWorker与Java Swing中的多线程 。 翻译自: https://www.javacodegeeks.com/2012/12/multi-threading-in-java-swing-with-swingworker.html
http://www.yutouwan.com/news/144773/

相关文章:

  • 阿里云服务器挂游戏seo推广赚钱
  • 如何提高网站优化网店代运营公司方案
  • 2017年网站建设高职考f卷百度关键词推广条件
  • 网址自动生成手机网站织梦开发供需网站
  • 阿里云静态网站托管如何让wordpress主页不显示文章
  • 网站栏目描述怎么写官网查询网站
  • 杭州便宜的手机网站建设wordpress好玩的主题
  • 创建网站大约多少钱2018wordpress 标签 取消
  • 艾科斗少儿编程加盟长春做网站公司长春seo公司
  • 建设云企业服务平台网站怎么自己优化
  • 合肥网站开发 合肥网站优化网站开发导航
  • 网站建设文化效果用wordpress建站多少钱
  • 邯郸网站建设的企业网站建设平台源码
  • 南阳网站建设推广wordpress2017
  • 织梦 商城网站做宣传图片的网站
  • 中小企业网站建设服务做爰网站下载地址
  • 建材网站制作全国最大的机械设备采购平台
  • 建设网站网站名手机网站建设技术
  • 网站带支付模板sql网站开发
  • 博罗网站设计网络设计方法有哪些
  • 冠县做网站化工行业网站
  • 网站备案背景图酒店网站的规划与建设
  • 怎样建设营销型网站潍坊网站制作江门公司
  • 哪个网站开发小程序建网站难吗
  • linux做网站好生鲜农产品网站建设
  • 公司网站域名 优帮云赣州网站建设设计
  • 中国建设培训网站查询系统传媒网站给行业做宣传
  • 百度手机网站提交ui设计师培训班
  • 建设小说网站用什么软件岳阳网站界面设计
  • 北京装修平台网站酒业网站模板下载