Full examples of FutureTask and futuretask
MainActivity is as follows:
Package cc. cv; import java. util. concurrent. futureTask; import android. OS. bundle; import android. app. activity;/*** Demo Description: * FutureTask uses the complete example ** FutureTask is a subclass of Runnable. When creating a Thread object, you can pass it as a parameter ** detailed steps: * 1 create a subclass CallableImpl that implements the Callable interface and overwrite the call () method * 2 create a FutureTask object with CallableImpl as the parameter * 3 create a subthread with FutureTask as the parameter, and the startup thread ** will call the () method in CallableImpl when the sub-thread runs. * However, you can use futureTask in the main thread. isDone () can be used to determine whether a subthread has completed its work * In the process, use futureTask. get () to obtain the running results of the sub-thread. More accurately, the results of the call () method are obtained. * You can regard FutureTask as the optimization and improvement of Thread threads. ** reference materials: * 1 http://uule.iteye.com/blog/1539084 * 2 http://lf6627926.iteye.com/blog/1538313 * 3 http://blog.csdn.net/kaiwii/article/details/6773971 **/public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. Layout. main); testFatureTask ();} private void testFatureTask () {try {CallableImpl callableImpl = new CallableImpl (); FutureTask <Integer> futureTask = new FutureTask <Integer> (callableImpl ); // enable the subthread new Thread (futureTask) in the main Thread ). start (); System. out. println ("subthread starts running"); // determines whether the subthread has completed the work in the main thread. while (! FutureTask. isDone () {System. out. println ("determining whether the Sub-thread has completed the work in the main thread"); System. out. println ("the sub-thread is still working ........... ");} // obtain the running result of the subthread in the main thread System. out. println ("the sub-thread stops running. Result:" + futureTask. get ();} catch (Exception e ){}}}
CallableImpl is as follows:
Package cc. cv; import java. util. concurrent. callable; public class CallableImpl implements Callable <Integer> {private final int COUNTER = 9527; public CallableImpl () {}@ Overridepublic Integer call () throws Exception {try {System. out. println ("... simulate time-consuming work in a subthread... thread name: "+ Thread. currentThread (). getName (); Thread. sleep (1000*5); System. out. println ("... simulate time-consuming work in a subthread... thread name: "+ Thread. currentThread (). getName (); Thread. sleep (1000*7); System. out. println ("... simulate time-consuming work in a subthread... thread name: "+ Thread. currentThread (). getName ();} catch (Exception e) {} return COUNTER ;}}
Main. xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></RelativeLayout>
FutureTask-like usage?
This class can cancel asynchronous computing. This class provides the basic implementation of Future by using the calculation start and cancel methods, the method for querying whether the calculation is complete, and the method for retrieving the calculation result. Results can be retrieved only when the calculation is complete. If the calculation is not completed, the get method is blocked. Once the calculation is complete, you cannot start or cancel the calculation again.
You can use FutureTask to wrap Callable or Runnable objects. Because FutureTask implements Runnable, you can submit FutureTask to Executor for execution.
In addition to being an independent class, this class also provides the protected function, which may be useful when creating a custom task class.
Use Filter to verify whether a user logs in. Complete example:
*/Package cn. techtiger. struts; import java. io. IOException; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletContext; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServle TResponse; import javax. servlet. http. httpSession; public class OnlineFilter extends HttpServlet implements javax. servlet. filter {private static final long serialVersionUID = 1L; public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {HttpServletRequest req = (HttpServletRequest) request; httpServletResponse res = (HttpServle TResponse) response; HttpSession session = req. getSession (); if (session. getAttribute ("user") = null) {System. out. println ("user has logged in. "); res. sendRedirect ("error. jsp "); return;} else {chain. doFilter (request, response) ;}} public void init (FilterConfig filterConfig) {System. out. println ("OnlineFilter initialized. ");} public void destroy () {System. out. println ("OnlineFilter destroied ") ;} This is web. xml configuration onlineFilter cn. techtiger. struts. onlineFilter onlineFilter u _*. jsp Note: url-pattern is used to match which URLs need to be filtered by this filter. The current configuration is to verify the url that starts with u. jsp file. Exit. jsp error. the jsp file tested by jsp. It starts with testFilter. jsp, this page has created a session, so when you access u_login.jsp, The filte ...... remaining full text>